﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;

namespace YArchitech.HVAC
{
	public class HYPipeConnect
	{
		private DialogResult IsDistanceOK(string szTilte)
		{
			return MessageBox.Show(szTilte + "值过小（过小有可能连接失败），是否采用推荐值？\n是：采用程序推荐值。\n否：采用设置值。\n取消：退出操作", "盈建科提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
		}

		public bool NewTakeOffFitting(IList<Pipe> pipes)
		{
			bool flag = false;
			bool result;
			try
			{
				if (pipes.Count < 2)
				{
					throw new System.ArgumentNullException("Pipe", "is null");
				}
				Document document = pipes[0].Document;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping);
				if (xyz != null)
				{
					if (hypiping.IsPointOnPipingCurve(xyz))
					{
						document.Create.NewTakeoffFitting(hypiping2.GetAppropriateConnector(xyz), hypiping.Piping);
						flag = true;
					}
					else if (hypiping2.IsPointOnPipingCurve(xyz))
					{
						document.Create.NewTakeoffFitting(hypiping.GetAppropriateConnector(xyz), hypiping2.Piping);
						flag = true;
					}
				}
				result = flag;
			}
			catch (Exception)
			{
				throw new Exception("创建管件连接失败！");
			}
			return result;
		}

		public FamilyInstance NewTakeOffFitting(IList<Pipe> pipes, IList<XYZ> xyzs)
		{
			FamilyInstance familyInstance = null;
			FamilyInstance result;
			try
			{
				if (pipes.Count < 2 || xyzs.Count < 2)
				{
					throw new System.ArgumentNullException("Pipe or XYZ", "is null");
				}
				Document document = pipes[0].Document;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0], xyzs[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1], xyzs[1]);
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping);
				if (xyz != null)
				{
					Pipe pipe = null;
					Connector connector = null;
					if (hypiping.IsPointOnPipingCurve(xyz))
					{
						pipe = hypiping.Piping;
						connector = hypiping2.GetAppropriateConnector(xyz);
					}
					else if (hypiping2.IsPointOnPipingCurve(xyz))
					{
						pipe = hypiping2.Piping;
						connector = hypiping.GetAppropriateConnector(xyz);
					}
					familyInstance = document.Create.NewTakeoffFitting(connector, pipe);
				}
				result = familyInstance;
			}
			catch (Exception)
			{
				throw new Exception("创建管件连接失败！");
			}
			return result;
		}

		private string GetUnionFittingsFamilyName(Pipe pipe)
		{
			string text = "过桥弯_丝接";
			if (pipe.PipeType != null)
			{
				text = pipe.PipeType.Name;
				if (text.Contains("承插") || text.Contains("粘接"))
				{
					text = "过桥弯_承插或粘接";
				}
				else if (text.Contains("法兰"))
				{
					text = "过桥弯_法兰";
				}
				else if (text.Contains("焊接"))
				{
					text = "过桥弯_焊接";
				}
				else if (text.Contains("卡箍"))
				{
					text = "过桥弯_卡箍";
				}
				else if (text.Contains("卡套"))
				{
					text = "过桥弯_卡套";
				}
				else if (text.Contains("卡压"))
				{
					text = "过桥弯_卡压";
				}
				else if (text.Contains("热熔"))
				{
					text = "过桥弯_热熔";
				}
				else if (text.Contains("丝接"))
				{
					text = "过桥弯_丝接";
				}
			}
			return text;
		}

		public bool NewUnionFittings(Pipe separatePipe, Pipe pipe, Autodesk.Revit.DB.Document doc, XYZ unitVector, ref string msg, ref List<Pipe> pipes)
		{
			if (!this.GetElementByCollision(separatePipe, (Autodesk.Revit.DB.BuiltInCategory)(-2008044), doc).Contains(pipe.Id))
			{
				return false;
			}
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(separatePipe);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe);
			new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			new YJKLine(hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
			XYZ defferentPlaneInstersection = hypiping.GetDefferentPlaneInstersection(pipe);
			if (defferentPlaneInstersection != null)
			{
				try
				{
					string unionFittingsFamilyName = this.GetUnionFittingsFamilyName(separatePipe);
					FamilySymbol familySymbolByName = YJKRevitTools.GetFamilySymbolByName("Fitting", unionFittingsFamilyName, doc, ref msg);
					familySymbolByName.Activate();
					if (familySymbolByName == null)
					{
						return false;
					}
					FamilyInstance familyInstance = doc.Create.NewFamilyInstance(defferentPlaneInstersection, familySymbolByName, (Autodesk.Revit.DB.Structure.StructuralType)0);
					doc.Regenerate();
					List<Connector> list = new List<Connector>();
					foreach (Connector item in familyInstance.GetConnectors())
					{
						list.Add(item);
					}
					Connector connector = list[0];
					Connector connector2 = list[1];
					connector.Radius = hypiping.StartConnector.Connector.Radius;
					connector2.Radius = hypiping.EndConnector.Connector.Radius;
					if (!this.JudgePipeLength(familyInstance, defferentPlaneInstersection, hypiping.StartConnector.Origin, hypiping.EndConnector.Origin, doc, pipe, ref msg))
					{
						return false;
					}
					SubTransaction subTransaction = new SubTransaction(doc);
					subTransaction.Start();
					pipes = hypiping.Separate(defferentPlaneInstersection);
					if (pipes == null)
					{
						msg = "两管道无交点！";
						subTransaction.RollBack();
						doc.Delete(familyInstance.Id);
						return false;
					}
					YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipes[0]);
					YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipes[1]);
					Connector closeConnector = hypiping3.GetCloseConnector(defferentPlaneInstersection, false);
					Connector closeConnector2 = hypiping4.GetCloseConnector(defferentPlaneInstersection, false);
					if (!this.RotateUnionFitting(familyInstance, pipe, unitVector, doc, closeConnector, pipes[0]))
					{
						msg = "无法旋转到指定位置！";
						subTransaction.RollBack();
						doc.Delete(familyInstance.Id);
						return false;
					}
					subTransaction.Commit();
					if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-closeConnector.CoordinateSystem.BasisZ))
					{
						connector.ConnectTo(closeConnector);
						connector2.ConnectTo(closeConnector2);
					}
					else
					{
						connector2.ConnectTo(closeConnector);
						connector.ConnectTo(closeConnector2);
					}
					doc.Regenerate();
					this.MoveUnionFitting(familyInstance, doc, connector, connector2);
					return true;
				}
				catch (Exception ex)
				{
					msg = ex.Message;
					return false;
				}
			}
			msg = "两管道不垂直！";
			return false;
		}

		public List<ElementId> GetElementByCollision(Element tagElement, BuiltInCategory needElement, Autodesk.Revit.DB.Document doc)
		{
			List<ElementId> list = new List<ElementId>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			ElementIntersectsElementFilter elementIntersectsElementFilter = new ElementIntersectsElementFilter(tagElement);
			filteredElementCollector.WherePasses(elementIntersectsElementFilter);
			foreach (Element element in filteredElementCollector)
			{
                if (element.Category.Id.IntegerValue == (int)needElement)
				{
					list.Add(element.Id);
				}
			}
			return list;
		}

		private bool JudgePipeLength(FamilyInstance fi, XYZ point, XYZ startPoint, XYZ endPoint, Autodesk.Revit.DB.Document doc, Pipe pipe, ref string msg)
		{
			double num = 0.0;
			if (fi.GetParameter("碰撞管外径") != null)
			{
				fi.GetParameter("碰撞管外径").Set(pipe.GetParameter("外径").AsDouble());
				num += fi.GetParameter("碰撞管外径").AsDouble() / 2.0;
			}
			if (fi.GetParameter("直段长度") != null)
			{
				num += fi.GetParameter("直段长度").AsDouble();
			}
			if (fi.GetParameter("接口长度") != null)
			{
				num += fi.GetParameter("接口长度").AsDouble();
			}
			num /= 2.0;
			if (point.DistanceTo(startPoint) <= num || point.DistanceTo(endPoint) <= num)
			{
				doc.Delete(fi.Id);
				msg = "管道长度太短，无法生成过桥弯！";
				return false;
			}
			return true;
		}

		private bool RotateUnionFitting(FamilyInstance fi, Pipe pipe, XYZ unitVector, Autodesk.Revit.DB.Document doc, Connector conn, Pipe pipe1)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			LocationPoint locationPoint = fi.Location as LocationPoint;
			XYZ xyz = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, locationPoint.Point.Z);
			if (YJKRevitTools.isRiserPipe(pipe))
			{
				Line line = Line.CreateUnbound(xyz, fi.HandOrientation);
				ElementTransformUtils.RotateElement(doc, fi.Id, line, Math.PI*.5);
				double twoXYVectorAngle = YJKRevitTools.GetTwoXYVectorAngle(fi.HandOrientation.CrossProduct(fi.FacingOrientation), unitVector);
				ElementTransformUtils.RotateElement(doc, fi.Id, hyline.Line, twoXYVectorAngle);
				if (!fi.HandOrientation.CrossProduct(fi.FacingOrientation).IsAlmostEqualTo(unitVector.Normalize(), 0.0001))
				{
					ElementTransformUtils.RotateElement(doc, fi.Id, hyline.Line, Math.PI);
				}
			}
			else if (!YJKRevitTools.isRiserPipe(pipe1))
			{
				XYZ xyz2 = new XYZ(0.0, 0.0, 1.0);
				Line line2 = Line.CreateUnbound(xyz, xyz2);
				double twoXYVectorAngle2 = YJKRevitTools.GetTwoXYVectorAngle(fi.HandOrientation, conn.CoordinateSystem.BasisZ);
				ElementTransformUtils.RotateElement(doc, fi.Id, line2, twoXYVectorAngle2);
				XYZ xyz3 = fi.HandOrientation.CrossProduct(fi.FacingOrientation);
				Line line3 = Line.CreateUnbound(xyz, fi.HandOrientation);
				if (!xyz3.IsAlmostEqualTo(unitVector.Normalize(), 0.0001))
				{
					ElementTransformUtils.RotateElement(doc, fi.Id, line3, Math.PI);
				}
			}
			else
			{
				XYZ xyz4 = new XYZ(0.0, 0.0, 1.0);
				Line line4 = Line.CreateUnbound(xyz, xyz4);
				double twoXYVectorAngle3 = YJKRevitTools.GetTwoXYVectorAngle(fi.FacingOrientation, hypiping.StartConnector.Connector.CoordinateSystem.BasisZ);
				ElementTransformUtils.RotateElement(doc, fi.Id, line4, twoXYVectorAngle3);
				ElementTransformUtils.RotateElement(doc, fi.Id, hyline.Line, Math.PI*.5);
				if (!fi.HandOrientation.CrossProduct(fi.FacingOrientation).IsAlmostEqualTo(unitVector.Normalize(), 0.0001))
				{
					ElementTransformUtils.RotateElement(doc, fi.Id, hyline.Line, Math.PI);
				}
			}
			return fi.HandOrientation.CrossProduct(fi.FacingOrientation).IsAlmostEqualTo(unitVector.Normalize(), 0.0001);
		}

		private void MoveUnionFitting(FamilyInstance fi, Autodesk.Revit.DB.Document doc, Connector fiConn1, Connector fiConn2)
		{
			LocationPoint locationPoint = fi.Location as LocationPoint;
			XYZ xyz = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, locationPoint.Point.Z);
			XYZ xyz2 = xyz.IsAlmostEqualTo(fiConn1.Origin) ? fiConn2.Origin : fiConn1.Origin;
			XYZ xyz3 = xyz.Add(xyz2) / 2.0;
			XYZ xyz4 = new XYZ(xyz.X - xyz3.X, xyz.Y - xyz3.Y, xyz.Z - xyz3.Z);
			fi.Location.Move(xyz4);
			doc.Regenerate();
		}

		public bool NewElbowConnect(Pipe verticalPipe, Pipe planePipe, Pipe midPipe, Autodesk.Revit.DB.Document doc, ref string msg)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(verticalPipe);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(planePipe);
			YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(midPipe);
			YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			YJKLine hyline2 = new YJKLine(hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
			XYZ xyz = hyline.IntersectionPoint(hyline2.Line, true);
			if (xyz == null)
			{
				msg = "两管道无交点！";
				return false;
			}
			if (verticalPipe.GetParameter("外径") == null || planePipe.GetParameter("外径") == null || midPipe.GetParameter("外径") == null)
			{
				msg = "管道的无外径参数！";
				return false;
			}
			Pipe pipe = (verticalPipe.GetParameter("外径").AsDouble() < planePipe.GetParameter("外径").AsDouble()) ? verticalPipe : planePipe;
			double num = pipe.GetParameter("外径").AsDouble() * 4.0;
			if (hyline.Line.Length > num && hyline2.Line.Length > num)
			{
				XYZ xyzIntersection = new XYZ();
				XYZ xyzIntersection2 = new XYZ();
				Pipe pipe2 = this.CreateNewPipe(hypiping, hypiping2, xyz, pipe, num, ref xyzIntersection, ref xyzIntersection2);
				YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipe2);
				double dppipeDistance = hypiping4.GetDPPipeDistance(midPipe);
				double num2 = (pipe2.GetParameter("外径").AsDouble() + midPipe.GetParameter("外径").AsDouble()) / 2.0;
				if (dppipeDistance < num2)
				{
					XYZ defferentPlaneInstersection = hypiping4.GetDefferentPlaneInstersection(midPipe);
					XYZ defferentPlaneInstersection2 = hypiping3.GetDefferentPlaneInstersection(pipe2);
					XYZ xyz2 = (defferentPlaneInstersection - defferentPlaneInstersection2).Normalize() * (num2 - dppipeDistance);
					pipe2.Location.Move(xyz2);
					num += (num2 - dppipeDistance) / Math.Sin(0.78539816339744828);
					if (hyline.Line.Length < num && hyline2.Line.Length < num)
					{
						msg = "管道的长度过短！";
						doc.Delete(pipe2.Id);
						return false;
					}
				}
				Connector closeConnector = hypiping4.GetCloseConnector(xyzIntersection, false);
				Connector closeConnector2 = hypiping4.GetCloseConnector(xyzIntersection2, false);
				Connector closeConnector3 = hypiping.GetCloseConnector(xyz, false);
				Connector closeConnector4 = hypiping2.GetCloseConnector(xyz, false);
				doc.Create.NewElbowFitting(closeConnector3, closeConnector);
				doc.Create.NewElbowFitting(closeConnector4, closeConnector2);
				doc.Regenerate();
				return true;
			}
			msg = "管道的长度过短！";
			return false;
		}

		private Pipe CreateNewPipe(YJKPiping<Pipe> hysPipe, YJKPiping<Pipe> hyePipe, XYZ interPoint, Pipe temPipe, double length, ref XYZ verPoint, ref XYZ planePoint)
		{
			YJKLine hyline = new YJKLine(hysPipe.StartConnector.Origin, hysPipe.EndConnector.Origin);
			YJKLine hyline2 = new YJKLine(hyePipe.StartConnector.Origin, hyePipe.EndConnector.Origin);
			XYZ xyz = (hysPipe.StartConnector.Origin - hysPipe.EndConnector.Origin).Normalize();
			if (hyline2.Line.Distance(hysPipe.EndConnector.Origin) > hyline2.Line.Distance(hysPipe.StartConnector.Origin))
			{
				xyz = (hysPipe.EndConnector.Origin - hysPipe.StartConnector.Origin).Normalize();
			}
			XYZ xyz2 = (hyePipe.StartConnector.Origin - hyePipe.EndConnector.Origin).Normalize();
			if (hyline.Line.Distance(hyePipe.EndConnector.Origin) > hyline.Line.Distance(hyePipe.StartConnector.Origin))
			{
				xyz2 = (hyePipe.EndConnector.Origin - hyePipe.StartConnector.Origin).Normalize();
			}
			verPoint = interPoint.Add(length * xyz);
			planePoint = interPoint.Add(length * xyz2);
			return YJKRevitTools.NewPipe(temPipe, verPoint, planePoint);
		}

		public void NewDuctAccessory(List<YJKPiping<Pipe>> lldu, XYZ intersectionPoint, ICollection<ElementId> fi, Autodesk.Revit.DB.Document doc)
		{
			if (lldu.Count < 2)
			{
				throw new System.ArgumentNullException("Duct is null");
			}
			YJKPiping<Pipe> hypiping = lldu[0];
			YJKPiping<Pipe> hypiping2 = lldu[1];
			new ConnectHelper();
			Connector closeConnector = hypiping.GetCloseConnector(intersectionPoint, false);
			Connector closeConnector2 = hypiping2.GetCloseConnector(intersectionPoint, false);
			IList<Connector> list = new List<Connector>();
			if (fi.Count > 0)
			{
				foreach (ElementId elementId in fi)
				{
					ConnectorSetIterator connectorSetIterator = (doc.GetElementById(elementId) as FamilyInstance).MEPModel.ConnectorManager.Connectors.ForwardIterator();
					int num = 0;
					while (connectorSetIterator.MoveNext())
					{
						num++;
						Connector connector = connectorSetIterator.Current as Connector;
						if (connector != null && !connector.IsConnected && !list.Contains(connector) && connector.Domain == (Autodesk.Revit.DB.Domain)3)
						{
							list.Add(connector);
						}
						if (num > 10)
						{
							break;
						}
					}
				}
			}
			Connector connector2 = this.getthenearstConnector(closeConnector, list);
			Connector connector3 = this.getthenearstConnector(closeConnector2, list);
			XYZ xyz = (closeConnector.Origin - closeConnector2.Origin).Normalize();
			closeConnector.ConnectTo(connector2);
			doc.Regenerate();
			closeConnector2.ConnectTo(connector3);
			doc.Regenerate();
			ElementTransformUtils.MoveElements(doc, fi, xyz * 1E-06);
			ElementTransformUtils.MoveElements(doc, fi, -1.0 * xyz * 1E-06);
			ElementTransformUtils.MoveElement(doc, hypiping2.Piping.Id, xyz * 1E-06);
			ElementTransformUtils.MoveElement(doc, hypiping2.Piping.Id, -1.0 * xyz * 1E-06);
			doc.Regenerate();
		}

		public Connector getthenearstConnector(Connector conn, IList<Connector> list)
		{
			List<double> list2 = new List<double>();
			foreach (Connector connector in list)
			{
				list2.Add(conn.Origin.DistanceTo(connector.Origin));
			}
			int index = list2.FindIndex((double a) => a == list2.Min());
			return list.ElementAt(index);
		}

		public bool NewElbowFitting(IList<Pipe> pipes, bool beReginerate = true)
		{
			bool flag = false;
			bool result;
			try
			{
				if (pipes.Count < 2)
				{
					throw new System.ArgumentNullException("Pipe", "is null");
				}
				Document document = pipes[0].Document;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping, 0.03);
				if (xyz != null)
				{
					Connector connector = null;
					Connector connector2 = null;
					this.GetOrderPipesConnectors(hypiping, hypiping2, xyz, ref connector, ref connector2);
					if (connector != null && connector2 != null && !connector.IsConnected && !connector2.IsConnected)
					{
						document.Create.NewElbowFitting(connector, connector2);
						flag = true;
					}
				}
				if (beReginerate)
				{
					document.Regenerate();
				}
				result = flag;
			}
			catch (Exception)
			{
				throw new Exception("创建管件连接失败，可能由于管道角度过大或者过小！");
			}
			return result;
		}

		public bool NewElbowFitting2(IList<Pipe> pipes)
		{
			bool flag = false;
			bool result;
			try
			{
				if (pipes.Count < 2)
				{
					throw new System.ArgumentNullException("Pipe", "is null");
				}
				Document document = pipes[0].Document;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping, 0.03);
				if (xyz != null)
				{
					Connector nearConnectorToPoint = hypiping.GetNearConnectorToPoint(xyz);
					Connector nearConnectorToPoint2 = hypiping2.GetNearConnectorToPoint(xyz);
					if (nearConnectorToPoint != null && nearConnectorToPoint2 != null && !nearConnectorToPoint.IsConnected && !nearConnectorToPoint2.IsConnected)
					{
						document.Create.NewElbowFitting(nearConnectorToPoint, nearConnectorToPoint2);
						flag = true;
					}
				}
				document.Regenerate();
				result = flag;
			}
			catch (Exception)
			{
				throw new Exception("创建管件连接失败，可能由于管道角度过大或者过小！");
			}
			return result;
		}

		public Pipe CreatVertailPipe(IList<Pipe> pipes, XYZ pnt)
		{
			XYZ startXYZ = this.OnPipeIntersectionPoint(pipes[0], pnt);
			XYZ endXYZ = this.OnPipeIntersectionPoint(pipes[1], pnt);
			Pipe pipe = YJKRevitTools.NewPipe(pipes[0], startXYZ, endXYZ);
			double num = pipes[0].GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
			pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(num);
			return pipe;
		}

		public Pipe GetNewPipe(Pipe pipe, XYZ InterPnt)
		{
			Pipe result = pipe;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			Connector connector = hypiping.StartConnector.Connector;
			Connector connector2 = hypiping.EndConnector.Connector;
			XYZ xyz = hypiping.StartConnector.Origin;
			XYZ xyz2 = hypiping.EndConnector.Origin;
			if (!connector.IsConnected && !connector2.IsConnected)
			{
				if (InterPnt.DistanceTo(xyz) < InterPnt.DistanceTo(xyz2))
				{
					xyz = InterPnt;
				}
				else
				{
					xyz2 = InterPnt;
				}
			}
			else
			{
				if (connector.IsConnected)
				{
					xyz2 = InterPnt;
				}
				if (connector2.IsConnected)
				{
					xyz = InterPnt;
				}
			}
			YJKRevitTools.ChangePipeSlop(ref result, xyz, xyz2);
			return result;
		}

		public IList<Connector> CreatSlopPipe(IList<Pipe> pipes)
		{
			IList<Connector> list = new List<Connector>();
			foreach (Pipe pipe in pipes)
			{
				foreach (object obj in pipe.ConnectorManager.Connectors)
				{
					Connector connector = (Connector)obj;
					if (connector.IsConnected)
					{
						Connector connector2 = connector;
						Connector connectConnector = YJKRevitTools.GetConnectConnector(connector2);
						connector2.DisconnectFrom(connectConnector);
						list.Add(connectConnector);
					}
				}
			}
			return list;
		}

		public void CreatConnPipe(IList<Connector> listConn, IList<Pipe> pipes)
		{
			foreach (Pipe pipe in pipes)
			{
				foreach (Connector connector in listConn)
				{
					foreach (object obj in pipe.ConnectorManager.Connectors)
					{
						Connector connector2 = (Connector)obj;
						if (connector.Origin.IsAlmostEqualTo(connector2.Origin))
						{
							connector.ConnectTo(connector2);
						}
						if ((connector.Origin * 0.99).IsAlmostEqualTo(connector2.Origin) || (connector.Origin * 1.001).IsAlmostEqualTo(connector2.Origin))
						{
							connector.ConnectTo(connector2);
						}
					}
				}
			}
		}

		public IList<Pipe> GetSlopElbowPipe(IList<Pipe> pipes, int index)
		{
			IList<Pipe> list = new List<Pipe>();
			if (index != 0 && index != 1)
			{
				return null;
			}
			Document document = pipes[index].Document;
			IList<XYZ> elementEndPoint = YJKRevitTools.GetElementEndPoint(pipes[index]);
			YJKLine hyline = new YJKLine(elementEndPoint[0], elementEndPoint[1]);
			int index2 = 0;
			if (index == 0)
			{
				index2 = 1;
			}
			IList<XYZ> elementEndPoint2 = YJKRevitTools.GetElementEndPoint(pipes[index2]);
			elementEndPoint[0].DistanceTo(elementEndPoint[1]);
			elementEndPoint2[0].DistanceTo(elementEndPoint2[1]);
			Line otherLine = document.CreatYJKLine(elementEndPoint2[0], elementEndPoint2[1], true);
			XYZ xyz = hyline.IntersectionPoint(otherLine, true);
			IList<XYZ> list2 = YJKRevitTools.OrderPipePoint(pipes[index], xyz);
			IList<XYZ> list3 = YJKRevitTools.OrderPipePoint(pipes[index2], xyz);
			double slopValue = YJKRevitTools.GetSlopValue(list2[0], list2[1], xyz);
			xyz = new XYZ(xyz.X, xyz.Y, list2[0].Z + slopValue);
			Pipe item = pipes[index];
			Pipe item2 = pipes[index2];
			YJKRevitTools.ChangePipeSlop(ref item, list2[0], xyz);
			YJKRevitTools.ChangePipeSlop(ref item2, list3[0], xyz);
			list.Add(item);
			list.Add(item2);
			return list;
		}

		public IList<Pipe> GetSlopTeePipe(IList<Pipe> pipes)
		{
			IList<Pipe> list = new List<Pipe>();
			Pipe pipe = pipes[0];
			Pipe pipe2 = pipes[1];
			Document document = pipe.Document;
			IList<XYZ> elementEndPoint = YJKRevitTools.GetElementEndPoint(pipe);
			YJKLine hyline = new YJKLine(elementEndPoint[0], elementEndPoint[1]);
			IList<XYZ> elementEndPoint2 = YJKRevitTools.GetElementEndPoint(pipe2);
			YJKLine hyline2 = new YJKLine(elementEndPoint2[0], elementEndPoint2[1]);
			Line otherLine = document.CreatYJKLine(elementEndPoint2[0], elementEndPoint2[1], true);
			XYZ xyz = hyline.IntersectionPoint(otherLine, true);
			bool flag = hyline.IsPointOnLine(xyz);
			bool flag2 = hyline2.IsPointOnLine(xyz);
			IList<Pipe> list2 = new List<Pipe>();
			if (!flag && !flag2)
			{
				return null;
			}
			if (flag)
			{
				XYZ xyz2 = this.OnPipeIntersectionPoint(pipe, xyz);
				IList<XYZ> list3 = YJKRevitTools.OrderPipePoint(pipe2, xyz);
				YJKRevitTools.ChangePipeSlop(ref pipe2, list3[0], xyz2);
				list.Add(pipe2);
				list2 = new YJKPiping<Pipe>(pipe).Separate(xyz2);
				list.Add(list2[0]);
				list.Add(list2[1]);
			}
			if (flag2)
			{
				XYZ xyz2 = this.OnPipeIntersectionPoint(pipe2, xyz);
				IList<XYZ> list4 = YJKRevitTools.OrderPipePoint(pipe, xyz);
				YJKRevitTools.ChangePipeSlop(ref pipe, list4[0], xyz2);
				list.Add(pipe);
				list2 = new YJKPiping<Pipe>(pipe2).Separate(xyz2);
				list.Add(list2[0]);
				list.Add(list2[1]);
			}
			return list;
		}

		public XYZ OnPipeIntersectionPoint(Pipe pipe, XYZ interPipe)
		{
			return new YJKLine(YJKRevitTools.GetElementCurve(pipe, 0), YJKRevitTools.GetElementCurve(pipe, 1)).GetVertial(interPipe);
		}

		public double GetMaxDiameter(IList<Pipe> listPipe)
		{
			double num = 0.0;
			int num2 = 0;
			foreach (Pipe elem in listPipe)
			{
				if (num2 == 0)
				{
					num = elem.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
					num2++;
				}
				double num3 = elem.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
				num = ((num3 > num) ? num3 : num);
			}
			return num;
		}

		public IList<Pipe> GetSlopFourPipe(IList<Pipe> pipes, int index)
		{
			IList<Pipe> list = new List<Pipe>();
			if (index != 0 && index != 1)
			{
				return null;
			}
			int index2 = 0;
			if (index == 0)
			{
				index2 = 1;
			}
			Pipe pipe = pipes[index];
			Pipe pipe2 = pipes[index2];
			Document document = pipe.Document;
			IList<XYZ> elementEndPoint = YJKRevitTools.GetElementEndPoint(pipe);
			YJKLine hyline = new YJKLine(elementEndPoint[0], elementEndPoint[1]);
			IList<XYZ> elementEndPoint2 = YJKRevitTools.GetElementEndPoint(pipe2);
			Line otherLine = document.CreatYJKLine(elementEndPoint2[0], elementEndPoint2[1], true);
			XYZ xyz = hyline.IntersectionPoint(otherLine);
			if (xyz == null)
			{
				return null;
			}
			XYZ xyz2 = this.OnPipeIntersectionPoint(pipe, xyz);
			IList<XYZ> slopPoint = YJKRevitTools.GetSlopPoint(pipe2, xyz2);
			YJKRevitTools.ChangePipeSlop(ref pipe2, slopPoint[0], slopPoint[1]);
			IList<Pipe> list2 = new List<Pipe>();
			list2 = new YJKPiping<Pipe>(pipe).Separate(xyz2);
			list.Add(list2[0]);
			list.Add(list2[1]);
			list2 = new YJKPiping<Pipe>(pipe2).Separate(xyz2);
			list.Add(list2[0]);
			list.Add(list2[1]);
			return list;
		}

		public string GetSlopConnStyle(IList<Pipe> pipes, ref XYZ pnt)
		{
			string result = "";
			Pipe pipe = pipes[0];
			Pipe elem = pipes[1];
			Document document = pipe.Document;
			IList<XYZ> elementEndPoint = YJKRevitTools.GetElementEndPoint(pipe);
			YJKLine hyline = new YJKLine(elementEndPoint[0], elementEndPoint[1]);
			Line otherLine = document.CreatYJKLine(elementEndPoint[0], elementEndPoint[1], true);
			IList<XYZ> elementEndPoint2 = YJKRevitTools.GetElementEndPoint(elem);
			YJKLine hyline2 = new YJKLine(elementEndPoint2[0], elementEndPoint2[1]);
			Line otherLine2 = document.CreatYJKLine(elementEndPoint2[0], elementEndPoint2[1], true);
			if (hyline.IsLineOnLine(otherLine2))
			{
				return "直线";
			}
			if (hyline.IsParallel(otherLine2))
			{
				return "平行";
			}
			pnt = hyline.IntersectionPoint(otherLine2);
			if (pnt != null)
			{
				if (hyline.IsNearEndPoint(pnt) && hyline2.IsNearEndPoint(pnt))
				{
					return "弯头";
				}
				if (hyline.IsNearEndPoint(pnt) || hyline2.IsNearEndPoint(pnt))
				{
					return "三通";
				}
				return "四通";
			}
			else
			{
				pnt = hyline.IntersectionPoint(true, otherLine2);
				if (pnt != null && hyline.IsNearEndPoint(pnt))
				{
					return "弯头";
				}
				if (pnt == null)
				{
					pnt = hyline2.IntersectionPoint(true, otherLine);
					if (pnt != null && hyline2.IsNearEndPoint(pnt))
					{
						return "弯头";
					}
				}
				if (pnt != null)
				{
					return "三通";
				}
				pnt = hyline.IntersectionPoint(otherLine2, true);
				if (pnt != null)
				{
					return "弯头";
				}
				return result;
			}
		}

		public FamilyInstance NewElbowFitting(IList<Pipe> pipes, IList<XYZ> xyzs)
		{
			FamilyInstance familyInstance = null;
			FamilyInstance result;
			try
			{
				if (pipes.Count < 2 || xyzs.Count < 2)
				{
					throw new System.ArgumentNullException("Pipe or XYZ", "is null");
				}
				Document document = pipes[0].Document;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0], xyzs[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1], xyzs[1]);
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping, 0.001);
				if (xyz != null)
				{
					Connector connector = null;
					Connector connector2 = null;
					this.GetOrderPipesConnectors(hypiping, hypiping2, xyz, ref connector, ref connector2);
					if (connector == null || connector2 == null || connector.IsConnected || connector2.IsConnected)
					{
						throw new Exception("水管已经连接");
					}
					familyInstance = document.Create.NewElbowFitting(connector, connector2);
				}
				result = familyInstance;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public FamilyInstance NewElbowFitting(IList<Pipe> pipes, IList<XYZ> xyzs, ref string connMSG)
		{
			FamilyInstance familyInstance = null;
			FamilyInstance result;
			try
			{
				if (pipes.Count < 2 || xyzs.Count < 2)
				{
					connMSG = "弯头连接，必须是两根水管！";
					result = familyInstance;
				}
				else
				{
					Document document = pipes[0].Document;
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0], xyzs[0]);
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1], xyzs[1]);
					XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping, 0.001);
					if (xyz == null)
					{
						connMSG = "两水管平行或者没有交点\n不能构成弯头连接条件！";
						result = familyInstance;
					}
					else
					{
						Connector appropriateConnector = hypiping.GetAppropriateConnector(xyz);
						Connector appropriateConnector2 = hypiping2.GetAppropriateConnector(xyz);
						if (appropriateConnector == null || appropriateConnector2 == null)
						{
							connMSG = "找不到水管的连接口！";
							result = familyInstance;
						}
						else
						{
							XYZ basisZ = appropriateConnector.CoordinateSystem.BasisZ;
							XYZ basisZ2 = appropriateConnector2.CoordinateSystem.BasisZ;
							double num = YJKRevitTools.GetTwoVectorAngle(basisZ, basisZ2, basisZ.CrossProduct(basisZ2).Normalize()) * 180.0 / Math.PI;
							if (num < 90.0)
							{
								connMSG = "两水管间的夹角不能小于90°！";
								result = familyInstance;
							}
							else if (num > 175.0 && num < 180.0)
							{
								connMSG = "两水管间夹角太大，不能进行连接！";
								result = familyInstance;
							}
							else if (num == 180.0)
							{
								connMSG = "两水管在同一直线上，\n不能进行弯头连接，\n请用“断线连接”功能进行连接！";
								result = familyInstance;
							}
							else if (appropriateConnector.IsConnected || appropriateConnector2.IsConnected)
							{
								connMSG = "水管已经连接，\n无法再次连接操作！";
								result = familyInstance;
							}
							else
							{
								familyInstance = document.Create.NewElbowFitting(appropriateConnector, appropriateConnector2);
								result = familyInstance;
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public FamilyInstance NewElbowFitting(IList<Pipe> pipes, IList<XYZ> xyzs, string elbowName, ref string connMSG)
		{
			FamilyInstance familyInstance = null;
			FamilyInstance result;
			try
			{
				if (pipes.Count < 2 || xyzs.Count < 2)
				{
					connMSG = "弯头连接，必须是两根水管！";
					result = familyInstance;
				}
				else
				{
					Document document = pipes[0].Document;
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0], xyzs[0]);
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1], xyzs[1]);
					XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping, 0.001);
					if (xyz == null)
					{
						connMSG = "两水管平行或者没有交点\n不能构成弯头连接条件！";
						result = familyInstance;
					}
					else
					{
						Connector appropriateConnector = hypiping.GetAppropriateConnector(xyz);
						Connector appropriateConnector2 = hypiping2.GetAppropriateConnector(xyz);
						if (appropriateConnector == null || appropriateConnector2 == null)
						{
							connMSG = "找不到水管的连接口！";
							result = familyInstance;
						}
						else
						{
							XYZ basisZ = appropriateConnector.CoordinateSystem.BasisZ;
							XYZ basisZ2 = appropriateConnector2.CoordinateSystem.BasisZ;
							double num = YJKRevitTools.GetTwoVectorAngle(basisZ, basisZ2, basisZ.CrossProduct(basisZ2).Normalize()) * 180.0 / Math.PI;
							if (num < 90.0)
							{
								connMSG = "两水管间的夹角不能小于90°！";
								result = familyInstance;
							}
							else if (num > 175.0 && num < 180.0)
							{
								connMSG = "两水管间夹角太大，不能进行连接！";
								result = familyInstance;
							}
							else if (num == 180.0)
							{
								connMSG = "两水管在同一直线上，\n不能进行弯头连接，\n请用“断线连接”功能进行连接！";
								result = familyInstance;
							}
							else if (appropriateConnector.IsConnected || appropriateConnector2.IsConnected)
							{
								connMSG = "水管已经连接，\n无法再次连接操作！";
								result = familyInstance;
							}
							else
							{
								ConnectPipeManage connectPipeManage = new ConnectPipeManage();
								if (string.IsNullOrEmpty(elbowName))
								{
									this.NewSomeConnect(pipes);
									result = null;
								}
								else
								{
									FamilySymbol familySymbolByName = connectPipeManage.GetFamilySymbolByName(elbowName, document, ref connMSG);
									if (!string.IsNullOrEmpty(connMSG))
									{
										result = familyInstance;
									}
									else
									{
										if (familySymbolByName != null)
										{
											familyInstance = document.Create.NewElbowFitting(appropriateConnector, appropriateConnector2);
											familyInstance.Symbol = familySymbolByName;
										}
										result = familyInstance;
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw new Exception("创建弯头失败，可能由于管道角度过大或者过小！");
			}
			return result;
		}

		public bool NewTransitionFitting(IList<Pipe> pipes, int beReginerate = 1)
		{
			bool flag = false;
			bool result;
			try
			{
				if (pipes.Count < 2)
				{
					throw new System.ArgumentNullException("Pipe", "is null");
				}
				Document document = pipes[0].Document;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
				new YJKPiping<Pipe>(pipes[1]);
				IList<Connector> list = new List<Connector>();
				list = hypiping.GetAppropriateConnector(pipes[1]);
				if (list.Count > 1 && list[0] != null && list[1] != null && !list[0].IsConnected && !list[1].IsConnected)
				{
					document.Create.NewTransitionFitting(list[0], list[1]);
					flag = true;
				}
				if (beReginerate == 1)
				{
					document.Regenerate();
				}
				result = flag;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public FamilyInstance NewTransitionFitting(IList<Pipe> pipes, bool resultFlag)
		{
			FamilyInstance result;
			try
			{
				if (pipes.Count < 2)
				{
					throw new System.ArgumentNullException("Pipe", "is null");
				}
				Document document = pipes[0].Document;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
				new YJKPiping<Pipe>(pipes[1]);
				IList<Connector> list = new List<Connector>();
				list = hypiping.GetAppropriateConnector(pipes[1]);
				if (list.Count <= 1)
				{
					throw new Exception("获取连接器失败");
				}
				if (list[0] == null || list[1] == null || list[0].IsConnected || list[1].IsConnected)
				{
					throw new Exception("水管已经连接");
				}
				if (list[0].Radius == list[1].Radius)
				{
					result = null;
				}
				else
				{
					FamilyInstance familyInstance = document.Create.NewTransitionFitting(list[0], list[1]);
					result = familyInstance;
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public FamilyInstance NewTransitionFitting(IList<Pipe> pipes, ref string connMSG)
		{
			FamilyInstance familyInstance = null;
			FamilyInstance result;
			try
			{
				if (pipes.Count != 2)
				{
					connMSG = "变径连接，仅支持两根水管的连接！";
					result = familyInstance;
				}
				else
				{
					Document document = pipes[0].Document;
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
					if (hypiping.GetDiameter() == hypiping2.GetDiameter())
					{
						connMSG = "两根水管直径相同，不能进行变径连接！";
						result = familyInstance;
					}
					else if (!hypiping.IsParallel(hypiping2.Piping) || !hypiping.IsPipingOnLine(hypiping2.Piping))
					{
						connMSG = "两根水管必须在同一直线上，否则无法连接！";
						result = familyInstance;
					}
					else
					{
						IList<Connector> list = new List<Connector>();
						list = hypiping.GetAppropriateConnector(pipes[1]);
						if (list.Count > 1)
						{
							if (list[0] != null && list[1] != null && !list[0].IsConnected && !list[1].IsConnected)
							{
								if (list[0].Radius == list[1].Radius)
								{
									result = null;
								}
								else
								{
									familyInstance = document.Create.NewTransitionFitting(list[0], list[1]);
									result = familyInstance;
								}
							}
							else
							{
								connMSG = "水管已经连接";
								result = familyInstance;
							}
						}
						else
						{
							connMSG = "获取连接器失败";
							result = familyInstance;
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public FamilyInstance NewTransitionFitting(IList<Pipe> pipes, string fsName, ref string connMSG)
		{
			FamilyInstance familyInstance = null;
			FamilyInstance result;
			try
			{
				if (pipes.Count != 2)
				{
					connMSG = "变径连接，仅支持两根水管的连接！";
					result = familyInstance;
				}
				else
				{
					Document document = pipes[0].Document;
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
					if (hypiping.GetDiameter() == hypiping2.GetDiameter())
					{
						connMSG = "两根水管直径相同，不能进行变径连接！";
						result = familyInstance;
					}
					else if (!hypiping.IsParallel(hypiping2.Piping) || !hypiping.IsPipingOnLine(hypiping2.Piping))
					{
						connMSG = "两根水管必须在同一直线上，否则无法连接！";
						result = familyInstance;
					}
					else
					{
						ConnectPipeManage connectPipeManage = new ConnectPipeManage();
						if (string.IsNullOrEmpty(fsName))
						{
							this.NewSomeConnect(pipes);
							result = null;
						}
						else
						{
							FamilySymbol familySymbolByName = connectPipeManage.GetFamilySymbolByName(fsName, document, ref connMSG);
							IList<Connector> list = new List<Connector>();
							list = hypiping.GetAppropriateConnector(pipes[1]);
							if (list.Count > 1)
							{
								if (list[0] != null && list[1] != null && !list[0].IsConnected && !list[1].IsConnected)
								{
									if (list[0].Radius == list[1].Radius)
									{
										result = null;
									}
									else
									{
										if (familySymbolByName != null)
										{
											familyInstance = document.Create.NewTransitionFitting(list[0], list[1]);
										}
										result = familyInstance;
									}
								}
								else
								{
									connMSG = "水管已经连接";
									result = familyInstance;
								}
							}
							else
							{
								connMSG = "获取连接器失败";
								result = familyInstance;
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public bool NewLHWFitting(IList<Pipe> pipes)
		{
			bool flag = false;
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (pipes.Count < 2)
				{
					throw new System.ArgumentNullException("Pipe", "is null");
				}
				Document document = pipes[0].Document;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<Connector> list = new List<Connector>();
				list = hypiping.GetAppropriateConnector(hypiping2.Piping);
				YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(YJKRevitTools.NewPipe(hypiping.Piping, list[0].Origin, list[1].Origin));
				Connector connector;
				Connector connector2;
				Connector connector3;
				Connector connector4;
				if (list[0].Origin.IsAlmostEqualTo(hypiping3.StartConnector.Origin))
				{
					connector = list[0];
					connector2 = hypiping3.StartConnector.Connector;
					connector3 = list[1];
					connector4 = hypiping3.EndConnector.Connector;
				}
				else
				{
					connector = list[0];
					connector2 = hypiping3.EndConnector.Connector;
					connector3 = list[1];
					connector4 = hypiping3.StartConnector.Connector;
				}
				if (connector != null && !connector.IsConnected && connector2 != null && !connector2.IsConnected && connector3 != null && !connector3.IsConnected && connector4 != null && !connector4.IsConnected)
				{
					document.Create.NewElbowFitting(connector, connector2);
					document.Create.NewElbowFitting(connector3, connector4);
					flag = true;
				}
				document.Regenerate();
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				throw new Exception("创建管件连接失败！");
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public IList<FamilyInstance> NewLHWFitting(IList<Pipe> pipes, bool resultFlag)
		{
			IList<FamilyInstance> list = new List<FamilyInstance>();
			SubTransaction subTransaction = null;
			IList<FamilyInstance> result;
			try
			{
				if (pipes.Count < 2)
				{
					throw new System.ArgumentNullException("Pipe", "is null");
				}
				Document document = pipes[0].Document;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<Connector> list2 = new List<Connector>();
				list2 = hypiping.GetAppropriateConnector(hypiping2.Piping);
				YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(YJKRevitTools.NewPipe(hypiping.Piping, list2[0].Origin, list2[1].Origin));
				Connector connector;
				Connector connector2;
				Connector connector3;
				Connector connector4;
				if (list2[0].Origin.IsAlmostEqualTo(hypiping3.StartConnector.Origin))
				{
					connector = list2[0];
					connector2 = hypiping3.StartConnector.Connector;
					connector3 = list2[1];
					connector4 = hypiping3.EndConnector.Connector;
				}
				else
				{
					connector = list2[0];
					connector2 = hypiping3.EndConnector.Connector;
					connector3 = list2[1];
					connector4 = hypiping3.StartConnector.Connector;
				}
				if (connector == null || connector.IsConnected || connector2 == null || connector2.IsConnected || connector3 == null || connector3.IsConnected || connector4 == null || connector4.IsConnected)
				{
					throw new Exception("水管已经连接");
				}
				list.Add(document.Create.NewElbowFitting(connector, connector2));
				list.Add(document.Create.NewElbowFitting(connector3, connector4));
				subTransaction.Commit();
				result = list;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public IList<FamilyInstance> NewBackFitting(IList<Pipe> pipes, ref string connMSG)
		{
			IList<FamilyInstance> list = new List<FamilyInstance>();
			SubTransaction subTransaction = null;
			IList<FamilyInstance> result;
			try
			{
				if (pipes.Count != 2)
				{
					connMSG = "来回弯连接仅支持两平行管道的连接，\n请坚持管道的选择数量！";
					result = null;
				}
				else
				{
					Document document = pipes[0].Document;
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
					if (hypiping.IsPipingOnLine(hypiping2.Piping))
					{
						connMSG = "两水管在同一直线上，不能进行来回弯连接！";
						result = null;
					}
					else if (!hypiping.IsParallel(hypiping2.Piping))
					{
						connMSG = "来回弯连接仅支持两平行管道的连接，\n请检查管道的位置关系！";
						result = null;
					}
					else
					{
						subTransaction = new SubTransaction(document);
						subTransaction.Start();
						IList<Connector> list2 = new List<Connector>();
						list2 = hypiping.GetAppropriateConnector(hypiping2.Piping);
						Connector connector = list2[0];
						Connector connector2;
						if (list2[0].Origin.IsAlmostEqualTo(hypiping.StartConnector.Origin))
						{
							connector2 = hypiping.EndConnector.Connector;
						}
						else
						{
							connector2 = hypiping.StartConnector.Connector;
						}
						Connector connector3 = list2[1];
						Connector connector4;
						if (list2[1].Origin.IsAlmostEqualTo(hypiping2.StartConnector.Origin))
						{
							connector4 = hypiping2.EndConnector.Connector;
						}
						else
						{
							connector4 = hypiping2.StartConnector.Connector;
						}
						XYZ xyz = connector.Origin.Subtract(connector2.Origin).Normalize();
						XYZ xyz2 = connector4.Origin.Subtract(connector3.Origin).Normalize();
						if (!xyz.IsAlmostEqualTo(xyz2))
						{
							connMSG = "两水管相近的两个接口无法进行连接,\n请检查管道的位置关系！";
							result = null;
						}
						else
						{
							XYZ basisZ = connector.CoordinateSystem.BasisZ;
							XYZ xyz3 = connector3.Origin.Subtract(connector.Origin).CrossProduct(basisZ).Normalize();
							XYZ xyz4 = basisZ.CrossProduct(xyz3).Normalize();
							XYZ xyz5 = basisZ.Add(xyz4).Normalize();
							Line otherLine = Line.CreateBound(connector.Origin, connector.Origin + 3.0 * xyz5);
							XYZ xyz6 = new YJKLine(Line.CreateBound(connector3.Origin, connector4.Origin)).IntersectionPoint(otherLine, 0.0003);
							if (xyz6 == null)
							{
								connMSG = "水管位置不满足连接条件，\n请检查管道的位置关系!";
								result = null;
							}
							else if (connector.Origin.DistanceTo(xyz6) > connector.Origin.DistanceTo(connector4.Origin))
							{
								connMSG = "水管位置不满足连接条件!";
								result = null;
							}
							else
							{
								ConnectPipeManage connectPipeManage = new ConnectPipeManage();
								string familySymbolName = connectPipeManage.GetFamilySymbolName(ConnectPipeStyle.Elbow);
								connectPipeManage.SetPipeElbowFitting(pipes, familySymbolName);
								double diameter = hypiping.GetDiameter();
								double diameter2 = hypiping2.GetDiameter();
								double num = (diameter <= diameter2) ? diameter2 : diameter;
								if (hypiping.GetDistance(hypiping2.Piping) < 1.5 * num)
								{
									connMSG = "两水管间间距偏小，不满足连接条件!";
									result = null;
								}
								else
								{
									YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(YJKRevitTools.NewPipe((diameter <= diameter2) ? hypiping.Piping : hypiping2.Piping, connector.Origin, xyz6));
									Connector connector5;
									Connector connector6;
									Connector connector7;
									Connector connector8;
									if (list2[0].Origin.IsAlmostEqualTo(hypiping3.StartConnector.Origin))
									{
										connector5 = connector;
										connector6 = hypiping3.StartConnector.Connector;
										connector7 = connector3;
										connector8 = hypiping3.EndConnector.Connector;
									}
									else
									{
										connector5 = connector;
										connector6 = hypiping3.EndConnector.Connector;
										connector7 = connector3;
										connector8 = hypiping3.StartConnector.Connector;
									}
									if (connector5 != null && !connector5.IsConnected && connector6 != null && !connector6.IsConnected && connector7 != null && !connector7.IsConnected && connector8 != null && !connector8.IsConnected)
									{
										list.Add(document.Create.NewElbowFitting(connector5, connector6));
										list.Add(document.Create.NewElbowFitting(connector7, connector8));
										FamilySymbol familySymbolByName = connectPipeManage.GetFamilySymbolByName(familySymbolName, document, ref connMSG);
										list[0].Symbol = familySymbolByName;
										list[1].Symbol = familySymbolByName;
										subTransaction.Commit();
										result = list;
									}
									else
									{
										connMSG = "水管已经连接";
										result = null;
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public bool NewTeeFitting(ref IList<Pipe> pipes, bool beReginerate = true)
		{
			bool flag = false;
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (pipes.Count < 3)
				{
					throw new System.ArgumentNullException("Pipe", "is null");
				}
				Document document = pipes[0].Document;
				new List<Pipe>();
				IList<Pipe> list = this.OrderPipePosition(pipes);
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(list[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(list[1]);
				YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(list[2]);
				YJKPiping<Pipe> hypiping4 = null;
				if (beReginerate)
				{
					subTransaction = new SubTransaction(document);
					subTransaction.Start();
				}
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping, 0.03);
				if (xyz != null)
				{
					if (hypiping2.Piping.UniqueId == hypiping3.Piping.UniqueId)
					{
						new List<Pipe>();
						hypiping4 = new YJKPiping<Pipe>(((IList<Pipe>)hypiping2.Separate(xyz))[1]);
					}
					else if (hypiping2.IsPipingOnLine(hypiping3.Piping))
					{
						hypiping4 = new YJKPiping<Pipe>(hypiping3.Piping);
					}
					if (hypiping4 != null)
					{
						Connector closeConnector = hypiping.GetCloseConnector(xyz, false);
						Connector connector = null;
						Connector connector2 = null;
						this.GetOrderPipesConnectors(hypiping2, hypiping4, xyz, ref connector, ref connector2);
						if (closeConnector != null && !closeConnector.IsConnected)
						{
							document.Create.NewTeeFitting(connector, connector2, closeConnector);
							flag = true;
						}
					}
					pipes.Clear();
					pipes.Add(hypiping.Piping);
					pipes.Add(hypiping2.Piping);
					pipes.Add(hypiping4.Piping);
				}
				if (beReginerate)
				{
					document.Regenerate();
					subTransaction.Commit();
				}
				result = flag;
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw new Exception("创建管件连接失败，可能由于管道角度过大或者过小！");
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public FamilyInstance NewTeeFitting(ref IList<Pipe> pipes, IList<XYZ> listPoints)
		{
			FamilyInstance familyInstance = null;
			SubTransaction subTransaction = null;
			FamilyInstance result;
			try
			{
				if (pipes.Count < 3)
				{
					throw new System.ArgumentNullException("Pipe", "is null");
				}
				Document document = pipes[0].Document;
				new List<Pipe>();
				IList<Pipe> list = this.OrderPipePosition(pipes);
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(list[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(list[1]);
				YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(list[2]);
				YJKPiping<Pipe> hypiping4 = null;
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping, 0.03);
				if (xyz != null)
				{
					if (hypiping2.Piping.UniqueId == hypiping3.Piping.UniqueId)
					{
						new List<Pipe>();
						hypiping4 = new YJKPiping<Pipe>(((IList<Pipe>)hypiping2.Separate(xyz))[1]);
					}
					else if (hypiping2.IsPipingOnLine(hypiping3.Piping))
					{
						hypiping4 = new YJKPiping<Pipe>(hypiping3.Piping);
					}
					if (hypiping4 != null)
					{
						Connector closeConnector = hypiping.GetCloseConnector(xyz, false);
						Connector connector = null;
						Connector connector2 = null;
						this.GetOrderPipesConnectors(hypiping2, hypiping4, xyz, ref connector, ref connector2);
						if (closeConnector != null && !closeConnector.IsConnected)
						{
							familyInstance = document.Create.NewTeeFitting(connector, connector2, closeConnector);
						}
					}
					pipes.Clear();
					pipes.Add(hypiping.Piping);
					pipes.Add(hypiping2.Piping);
					pipes.Add(hypiping4.Piping);
				}
				result = familyInstance;
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw new Exception("创建管件连接失败，可能由于管道角度过大或者过小！");
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		private IList<Pipe> OrderPipePosition(IList<Pipe> pipes)
		{
			IList<Pipe> list = new List<Pipe>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
			YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipes[2]);
			if (3 == pipes.Count)
			{
				if (hypiping.IsPipingOnLine(hypiping2.Piping) || hypiping.IsXYParallel(hypiping2.Piping))
				{
					list.Add(hypiping3.Piping);
					list.Add(hypiping.Piping);
					list.Add(hypiping2.Piping);
				}
				else if (hypiping.IsPipingOnLine(hypiping3.Piping) || hypiping.IsXYParallel(hypiping3.Piping))
				{
					list.Add(hypiping2.Piping);
					list.Add(hypiping.Piping);
					list.Add(hypiping3.Piping);
				}
				else
				{
					list = pipes;
				}
			}
			else if (4 == pipes.Count)
			{
				YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipes[3]);
				if (hypiping.IsPipingOnLine(hypiping2.Piping) || hypiping.IsXYParallel(hypiping2.Piping))
				{
					list = pipes;
				}
				else if (hypiping.IsPipingOnLine(hypiping3.Piping) || hypiping.IsXYParallel(hypiping3.Piping))
				{
					list.Add(hypiping.Piping);
					list.Add(hypiping3.Piping);
					list.Add(hypiping2.Piping);
					list.Add(hypiping4.Piping);
				}
				else if (hypiping.IsPipingOnLine(hypiping4.Piping) || hypiping.IsXYParallel(hypiping4.Piping))
				{
					list.Add(hypiping.Piping);
					list.Add(hypiping4.Piping);
					list.Add(hypiping2.Piping);
					list.Add(hypiping3.Piping);
				}
				else
				{
					list = pipes;
				}
			}
			return list;
		}

		private IList<Pipe> OrderPipePosition(IList<Pipe> pipes, ref IList<XYZ> xyzs)
		{
			IList<Pipe> list = new List<Pipe>();
			IList<XYZ> list2 = new List<XYZ>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0], xyzs[0]);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1], xyzs[1]);
			YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipes[2], xyzs[2]);
			if (3 == pipes.Count)
			{
				if (hypiping.IsPipingOnLine(hypiping2.Piping) || hypiping.IsParallel(hypiping2.Piping))
				{
					list.Add(hypiping3.Piping);
					list.Add(hypiping.Piping);
					list.Add(hypiping2.Piping);
					list2.Add(hypiping3.GlobalPoint);
					list2.Add(hypiping.GlobalPoint);
					list2.Add(hypiping2.GlobalPoint);
				}
				else if (hypiping.IsPipingOnLine(hypiping3.Piping) || hypiping.IsPipingOnLine(hypiping3.Piping))
				{
					list.Add(hypiping2.Piping);
					list.Add(hypiping.Piping);
					list.Add(hypiping3.Piping);
					list2.Add(hypiping2.GlobalPoint);
					list2.Add(hypiping.GlobalPoint);
					list2.Add(hypiping3.GlobalPoint);
				}
				else
				{
					list = pipes;
					list2 = xyzs;
				}
			}
			else if (4 == pipes.Count)
			{
				YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipes[3]);
				if (hypiping.IsPipingOnLine(hypiping2.Piping))
				{
					list = pipes;
					list2 = xyzs;
				}
				else if (hypiping.IsPipingOnLine(hypiping3.Piping))
				{
					list.Add(hypiping.Piping);
					list.Add(hypiping3.Piping);
					list.Add(hypiping2.Piping);
					list.Add(hypiping4.Piping);
					list2.Add(hypiping.GlobalPoint);
					list2.Add(hypiping3.GlobalPoint);
					list2.Add(hypiping2.GlobalPoint);
					list2.Add(hypiping4.GlobalPoint);
				}
				else if (hypiping.IsPipingOnLine(hypiping4.Piping))
				{
					list.Add(hypiping.Piping);
					list.Add(hypiping4.Piping);
					list.Add(hypiping2.Piping);
					list.Add(hypiping3.Piping);
					list2.Add(hypiping.GlobalPoint);
					list2.Add(hypiping4.GlobalPoint);
					list2.Add(hypiping2.GlobalPoint);
					list2.Add(hypiping3.GlobalPoint);
				}
				else
				{
					list = pipes;
					list2 = xyzs;
				}
			}
			xyzs = list2;
			return list;
		}

		public FamilyInstance NewTeeFitting(IList<Pipe> pipes, IList<XYZ> xyzs, FamilySymbol fs)
		{
			FamilyInstance familyInstance = null;
			SubTransaction subTransaction = null;
			FamilyInstance result;
			try
			{
				if (pipes.Count < 3 || xyzs.Count < 3)
				{
					throw new System.ArgumentNullException("Pipe", "is null");
				}
				Document document = pipes[0].Document;
				new List<Pipe>();
				this.OrderPipePosition(pipes, ref xyzs);
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0], xyzs[2]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1], xyzs[0]);
				YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipes[2], xyzs[1]);
				YJKPiping<Pipe> hypiping4 = null;
				hypiping.ChangeFirstTee(fs);
				hypiping2.ChangeFirstTee(fs);
				hypiping3.ChangeFirstTee(fs);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping, 0.001);
				if (xyz != null)
				{
					if (pipes[1].UniqueId == pipes[2].UniqueId)
					{
						new List<Pipe>();
						hypiping4 = new YJKPiping<Pipe>(((IList<Pipe>)hypiping2.Separate(xyz))[1], xyzs[2]);
					}
					else if (hypiping2.IsPipingOnLine(pipes[2]))
					{
						hypiping4 = new YJKPiping<Pipe>(pipes[2], xyzs[2]);
					}
					if (hypiping4 == null)
					{
						throw new Exception("请检查水管位置是否正确");
					}
					Connector appropriateConnector = hypiping.GetAppropriateConnector(xyz);
					Connector appropriateConnector2 = hypiping2.GetAppropriateConnector(xyz);
					Connector appropriateConnector3 = hypiping4.GetAppropriateConnector(xyz);
					if (appropriateConnector == null || appropriateConnector.IsConnected || appropriateConnector2 == null || appropriateConnector2.IsConnected || appropriateConnector3 == null || appropriateConnector3.IsConnected)
					{
						throw new Exception("水管已经连接");
					}
					familyInstance = document.Create.NewTeeFitting(appropriateConnector2, appropriateConnector3, appropriateConnector);
				}
				subTransaction.Commit();
				result = familyInstance;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private string JudgePipePosition(IList<Pipe> pipes, ref IList<XYZ> intersctionPnts)
		{
			if (pipes.Count != 4)
			{
				return "连接水管，必须为四根！";
			}
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
			YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipes[2]);
			YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipes[3]);
			if (!hypiping.IsPipingOnLine(hypiping2.Piping))
			{
				return "入口水管与出口水管不在同一直线上！";
			}
			XYZ xyz = hypiping.IntersectionPoint(hypiping3.Piping, 0.02);
			if (xyz == null)
			{
				return "支管1与入口管没有交点！";
			}
			XYZ xyz2 = hypiping.IntersectionPoint(hypiping4.Piping, 0.02);
			if (xyz2 == null)
			{
				return "支管2与入口管没有交点！";
			}
			if (Math.Abs(xyz.X - xyz2.X) > 0.0001 || Math.Abs(xyz.Y - xyz2.Y) > 0.0001)
			{
				return "两条支管与立管交点，不在同一直线上！";
			}
			intersctionPnts = new List<XYZ>();
			if (Math.Abs(xyz.Z - xyz2.Z) < 0.0001)
			{
				intersctionPnts.Add(xyz);
			}
			else
			{
				intersctionPnts.Add(xyz);
				intersctionPnts.Add(xyz2);
			}
			return "";
		}

		private IList<Pipe> AdjustPipePosition(IList<Pipe> pipes, bool beAdjust, ref XYZ intersctPnt)
		{
			IList<Pipe> list = new List<Pipe>();
			IList<XYZ> list2 = new List<XYZ>();
			string text = this.JudgePipePosition(pipes, ref list2);
			if (!string.IsNullOrEmpty(text))
			{
				throw new Exception(text);
			}
			if (list2.Count == 1)
			{
				intersctPnt = list2[0];
			}
			else if (list2.Count == 2 && beAdjust)
			{
				intersctPnt = list2[0];
			}
			if (intersctPnt == null)
			{
				return null;
			}
			Document document = pipes[0].Document;
			Pipe newPipe = YJKRevitTools.getNewPipe(pipes[0], intersctPnt, document);
			Connector connector = YJKRevitTools.SeparatePipeEndPoint(newPipe);
			if (connector != null)
			{
				new YJKPiping<Pipe>(newPipe).ConnectCloseConnector(connector);
			}
			list.Add(newPipe);
			newPipe = YJKRevitTools.getNewPipe(pipes[1], intersctPnt, document);
			connector = YJKRevitTools.SeparatePipeEndPoint(newPipe);
			if (connector != null)
			{
				new YJKPiping<Pipe>(newPipe).ConnectCloseConnector(connector);
			}
			list.Add(newPipe);
			newPipe = YJKRevitTools.getNewPipe(pipes[2], intersctPnt, document);
			connector = YJKRevitTools.SeparatePipeEndPoint(newPipe);
			if (connector != null)
			{
				new YJKPiping<Pipe>(newPipe).ConnectCloseConnector(connector);
			}
			list.Add(newPipe);
			newPipe = YJKRevitTools.getNewPipe(pipes[3], intersctPnt, document);
			connector = YJKRevitTools.SeparatePipeEndPoint(newPipe);
			if (connector != null)
			{
				new YJKPiping<Pipe>(newPipe).ConnectCloseConnector(connector);
			}
			list.Add(newPipe);
			document.Regenerate();
			return list;
		}

		public List<Pipe> NewDownCrossFitting(IList<Pipe> pipes, FamilySymbol fs)
		{
			List<Pipe> list = new List<Pipe>();
			SubTransaction subTransaction = null;
			try
			{
				if (pipes.Count != 4)
				{
					throw new System.ArgumentNullException("Pipe", "Incorrect number of pipes");
				}
				Document document = pipes[0].Document;
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				XYZ xyz = null;
				IList<Pipe> list2 = this.AdjustPipePosition(pipes, true, ref xyz);
				if (list2 == null || list2.Count < 1)
				{
					throw new Exception("水管位置不正确，无法连接");
				}
				UnifiedModified.ActivateFamilySymbol(fs);
				FamilyInstance fi = document.Create.NewFamilyInstance(xyz, fs, (Autodesk.Revit.DB.Structure.StructuralType)0);
				document.Regenerate();
				XYZ xyz2 = this.GetPipeEndPoint(list2[0], xyz);
				XYZ xyz3 = xyz2.Subtract(xyz).Normalize();
				XYZ xyz4 = this.GetPipeEndPoint(list2[2], xyz).Subtract(xyz).Normalize();
				XYZ xyz5 = this.GetPipeEndPoint(list2[3], xyz).Subtract(xyz).Normalize();
				this.SettingsYCrossFitting(fi, list2[0], 0.0, 0);
				this.SettingsYCrossFitting(fi, list2[0], 0.0, 1);
				double num = YJKRevitTools.GetTwoVectorAngle(xyz4, xyz3, xyz4.CrossProduct(xyz3).Normalize()) * 180.0 / Math.PI;
				this.SettingsYCrossFitting(fi, list2[2], num, 3);
				num = YJKRevitTools.GetTwoVectorAngle(xyz5, xyz3, xyz5.CrossProduct(xyz3).Normalize()) * 180.0 / Math.PI;
				this.SettingsYCrossFitting(fi, list2[3], num, 2);
				document.Regenerate();
				xyz2 = this.GetPipeEndPoint(list2[1], xyz);
				xyz3 = xyz2.Subtract(xyz);
				num = YJKRevitTools.GetTwoVectorAngle(xyz.Add(XYZ.BasisX), xyz3, new XYZ(0.0, 1.0, 0.0)) * 180.0 / Math.PI;
				YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
				hyrotateFamilyInstance.RotateElem(fi, xyz, new XYZ(0.0, 1.0, 0.0), 270.0);
				xyz2 = this.GetPipeEndPoint(list2[2], xyz);
				xyz3 = new XYZ(0.0, -1.0, 0.0);
				xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0);
				xyz4 = xyz2.Subtract(new XYZ(xyz.X, xyz.Y, 0.0)).Normalize();
				num = YJKRevitTools.GetTwoXYVectorAngle(xyz3, xyz4) * 180.0 / Math.PI;
				hyrotateFamilyInstance.RotateElem(document, fi, num);
				hyrotateFamilyInstance.MoveElem(fi, xyz);
				document.Regenerate();
				this.ConnectPipeFitting(fi, list2[0]);
				this.ConnectPipeFitting(fi, list2[1]);
				this.ConnectPipeFitting(fi, list2[3]);
				this.ConnectPipeFitting(fi, list2[2]);
				document.Regenerate();
				foreach (Pipe item in pipes)
				{
					list.Add(item);
				}
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return list;
		}

		private void ConnectPipeFitting(FamilyInstance fi, Pipe pipe)
		{
			XYZ point = (fi.Location as LocationPoint).Point;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			Connector closeConnector = hypiping.GetCloseConnector(point, false);
			Connector distanceConnector = hypiping.GetDistanceConnector(point);
			Connector nearConnector = new ConnectHelper().GetNearConnector(fi, distanceConnector);
			if (!this.IsSameConnector(closeConnector, nearConnector))
			{
				throw new Exception("连接口接头不匹配");
			}
			hypiping.Extend(nearConnector);
		}

		private void SettingsYCrossFitting(FamilyInstance fi, Pipe pipe, double angle, int i)
		{
			angle = angle / 180.0 * Math.PI;
			Parameter parameter = pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM);
			switch (i)
			{
			case 0:
			{
				Parameter parameter2 = fi.GetParameter("入口直径");
				if (parameter2 != null && !parameter2.IsReadOnly)
				{
					parameter2.Set(parameter.AsDouble());
					return;
				}
				break;
			}
			case 1:
			{
				Parameter parameter2 = fi.GetParameter("出口直径");
				if (parameter2 != null && !parameter2.IsReadOnly)
				{
					parameter2.Set(parameter.AsDouble());
					return;
				}
				break;
			}
			case 2:
			{
				Parameter parameter2 = fi.GetParameter("支管1直径");
				if (parameter2 != null && !parameter2.IsReadOnly)
				{
					parameter2.Set(parameter.AsDouble());
				}
				Parameter parameter3 = fi.GetParameter("支管1角度");
				if (parameter3 != null && !parameter3.IsReadOnly)
				{
					parameter3.Set(angle);
					return;
				}
				break;
			}
			case 3:
			{
				Parameter parameter2 = fi.GetParameter("支管2直径");
				if (parameter2 != null && !parameter2.IsReadOnly)
				{
					parameter2.Set(parameter.AsDouble());
				}
				Parameter parameter3 = fi.GetParameter("支管2角度");
				if (parameter3 != null && !parameter3.IsReadOnly)
				{
					parameter3.Set(angle);
				}
				break;
			}
			default:
				return;
			}
		}

		private XYZ GetPipeEndPoint(Pipe pipe, XYZ point)
		{
			XYZ xyz = new XYZ(0.0, 0.0, 0.0);
			LocationCurve locationCurve = pipe.Location as LocationCurve;
			XYZ hyendPoint = locationCurve.Curve.GetEndPoint(0);
			XYZ hyendPoint2 = locationCurve.Curve.GetEndPoint(1);
			if (hyendPoint.IsAlmostEqualTo(xyz))
			{
				xyz = hyendPoint2;
			}
			else
			{
				xyz = hyendPoint;
			}
			return xyz;
		}

		public FamilyInstance NewTeeFittingY(IList<Pipe> pipes, IList<XYZ> xyzs, FamilySymbol fs)
		{
			SubTransaction subTransaction = null;
			FamilyInstance familyInstance = null;
			Document document = null;
			FamilyInstance result;
			try
			{
				if (pipes.Count < 3 || xyzs.Count < 3)
				{
					throw new System.ArgumentNullException("Pipe", "is null");
				}
				document = pipes[0].Document;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0], xyzs[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1], xyzs[1]);
				YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipes[2], xyzs[2]);
				ConnectHelper connectHelper = new ConnectHelper();
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				XYZ xyz = hypiping.IntersectionPoint(hypiping3.Piping, 1);
				if (xyz == null)
				{
					throw new Exception("请检查水管位置是否正确");
				}
				YJKPiping<Pipe> hypiping4;
				if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
				{
					new List<Pipe>();
					hypiping4 = new YJKPiping<Pipe>(((IList<Pipe>)hypiping.Separate(xyz))[1], hypiping2.GlobalPoint);
					document.Regenerate();
				}
				else
				{
					hypiping4 = new YJKPiping<Pipe>(hypiping2.Piping, hypiping2.GlobalPoint);
				}
				UnifiedModified.ActivateFamilySymbol(fs);
				FamilyInstance familyInstance2 = document.Create.NewFamilyInstance(xyz, fs, (Autodesk.Revit.DB.Structure.StructuralType)0);
				document.Regenerate();
				if (familyInstance2.GetParameter("入口直径") == null)
				{
					subTransaction.RollBack();
					subTransaction = null;
					result = this.NewTeeFitting(pipes, xyzs, fs);
				}
				else
				{
					Parameter parameter = hypiping.Piping.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM);
					if (parameter == null)
					{
						throw new Exception("水管直径未找到");
					}
					if (!parameter.IsReadOnly)
					{
						familyInstance2.GetParameter("入口直径").Set(parameter.AsDouble());
					}
					this.SettingTeeParameter(familyInstance2, hypiping, hypiping3, 2);
					this.SettingTeeParameter(familyInstance2, hypiping, hypiping4, 1);
					document.Regenerate();
					XYZ point = (familyInstance2.Location as LocationPoint).Point;
					Connector closeConnector = hypiping.GetCloseConnector(point, false);
					Connector connector = connectHelper.GetTeeMainConnector(familyInstance2);
					double num = 0.0;
					Line line = Line.CreateBound(point, point + new XYZ(0.0, 0.0, 1.0));
					line.MakeUnbound();
					if (connector != null && closeConnector != null && !connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-closeConnector.CoordinateSystem.BasisZ))
					{
						num = closeConnector.CoordinateSystem.BasisZ.AngleTo(-connector.CoordinateSystem.BasisZ);
					}
					if (num > 0.0001)
					{
						ElementTransformUtils.RotateElement(document, familyInstance2.Id, line, num);
						document.Regenerate();
						if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connector.CoordinateSystem.BasisZ))
						{
							ElementTransformUtils.RotateElement(document, familyInstance2.Id, line, Math.PI*2 - 2.0 * num);
							document.Regenerate();
						}
					}
					Connector closeConnector2 = hypiping3.GetCloseConnector(point, false);
					Connector distanceConnector = hypiping3.GetDistanceConnector(point);
					Connector nearConnector = connectHelper.GetNearConnector(familyInstance2, distanceConnector);
					if (!this.IsSameConnector(closeConnector2, nearConnector))
					{
						Line line2 = Line.CreateBound(point, connector.Origin);
						line2.MakeUnbound();
						ElementTransformUtils.RotateElement(document, familyInstance2.Id, line2, Math.PI);
					}
					document.Regenerate();
					Connector distanceConnector2 = hypiping.GetDistanceConnector(point);
					connector = connectHelper.GetNearConnector(familyInstance2, distanceConnector2);
					if (!this.IsSameConnector(closeConnector, connector))
					{
						throw new Exception("入口接头不匹配");
					}
					Connector closeConnector3 = hypiping4.GetCloseConnector(point, false);
					Connector distanceConnector3 = hypiping4.GetDistanceConnector(point);
					Connector nearConnector2 = connectHelper.GetNearConnector(familyInstance2, distanceConnector3);
					if (!this.IsSameConnector(closeConnector3, nearConnector2))
					{
						throw new Exception("出口接头不匹配");
					}
					distanceConnector = hypiping3.GetDistanceConnector(point);
					nearConnector = connectHelper.GetNearConnector(familyInstance2, distanceConnector);
					if (!this.IsSameConnector(closeConnector2, nearConnector))
					{
						throw new Exception("支管接头不匹配");
					}
					hypiping.Extend(connector);
					hypiping3.Extend(nearConnector);
					hypiping4.Extend(nearConnector2);
					document.Regenerate();
					FamilyInstance familyInstance3 = familyInstance2;
					subTransaction.Commit();
					result = familyInstance3;
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (familyInstance != null)
				{
					document.Delete(familyInstance.Id);
				}
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public FamilyInstance NewTeeFittingConn(IList<Pipe> pipes, FamilySymbol fs, ref string connMSG)
		{
			SubTransaction subTransaction = null;
			FamilyInstance familyInstance = null;
			new List<Pipe>();
			string message = "";
			try
			{
				if (pipes.Count != 3)
				{
					message = "三通连接，必须选择三根水管！";
					return null;
				}
				Document document = pipes[0].Document;
				IList<Pipe> list = this.SortThreePipes(pipes, out message);
				if (list == null)
				{
					throw new Exception(message);
				}
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(list[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(list[1]);
				YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(list[2]);
				YJKLine hyline = new YJKLine((list[0].Location as LocationCurve).Curve as Line);
				YJKLine hyline2 = new YJKLine((list[2].Location as LocationCurve).Curve as Line);
				new ConnectHelper();
				XYZ xyz = hyline.IntersectionPoint(hyline2.Line, 0.016404199475065617);
				if (xyz == null)
				{
					message = "三根水管没有共同交点，不能进行连接！";
					return null;
				}
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				YJKPiping<Pipe> hypiping4;
				if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
				{
					new List<Pipe>();
					List<Pipe> list2 = hypiping2.Separate(xyz);
					hypiping = new YJKPiping<Pipe>(((IList<Pipe>)list2)[0]);
					hypiping4 = new YJKPiping<Pipe>(((IList<Pipe>)list2)[1]);
				}
				else
				{
					hypiping4 = new YJKPiping<Pipe>(hypiping2.Piping);
				}
				List<YJKPiping<Pipe>> list3 = new List<YJKPiping<Pipe>>();
				list3.Add(hypiping);
				list3.Add(hypiping4);
				list3.Add(hypiping3);
				if (YJKRevitTools.BeSlopePipe(hypiping.Piping) || YJKRevitTools.BeSlopePipe(hypiping3.Piping) || YJKRevitTools.BeSlopePipe(hypiping4.Piping))
				{
					familyInstance = this.TeeFittingConnSlopePipe(list3, fs, xyz, ref connMSG);
					if (familyInstance == null)
					{
						return null;
					}
				}
				else
				{
					familyInstance = this.TeeFittingConnPipe(list3, fs, xyz, ref connMSG);
					if (familyInstance == null)
					{
						return null;
					}
				}
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return familyInstance;
		}

		private FamilyInstance TeeFittingConnPipe(List<YJKPiping<Pipe>> pipes, FamilySymbol fs, XYZ intersectionPoint, ref string connMSG)
		{
			FamilyInstance familyInstance = null;
			try
			{
				Document document = pipes[0].Piping.Document;
				YJKPiping<Pipe> hypiping = pipes[0];
				YJKPiping<Pipe> hypiping2 = pipes[1];
				YJKPiping<Pipe> hypiping3 = pipes[2];
				Connector closeConnector = hypiping.GetCloseConnector(intersectionPoint, false);
				hypiping.GetDistanceConnector(intersectionPoint);
				Connector closeConnector2 = hypiping2.GetCloseConnector(intersectionPoint, false);
				Connector distanceConnector = hypiping2.GetDistanceConnector(intersectionPoint);
				Connector closeConnector3 = hypiping3.GetCloseConnector(intersectionPoint, false);
				Connector distanceConnector2 = hypiping3.GetDistanceConnector(intersectionPoint);
				if (closeConnector.IsConnected || closeConnector2.IsConnected || closeConnector3.IsConnected)
				{
					connMSG = "水管已连接！";
					return null;
				}
				UnifiedModified.ActivateFamilySymbol(fs);
				familyInstance = document.Create.NewFamilyInstance(intersectionPoint, fs, (Autodesk.Revit.DB.Structure.StructuralType)0);
				document.Regenerate();
				XYZ outVector = distanceConnector.Origin.Subtract(closeConnector2.Origin).Normalize();
				XYZ sideVector = distanceConnector2.Origin.Subtract(closeConnector3.Origin).Normalize();
				this.BranchPipeAngleSet(familyInstance, outVector, sideVector);
				ConnectHelper connectHelper = new ConnectHelper();
				Connector teeMainConnector = connectHelper.GetTeeMainConnector(familyInstance);
				Connector teeOutConnector = connectHelper.GetTeeOutConnector(familyInstance, teeMainConnector.CoordinateSystem.BasisZ);
				Connector teeSideConnector = connectHelper.GetTeeSideConnector(familyInstance, teeMainConnector.CoordinateSystem.BasisZ);
				this.TeeFittingSetting(familyInstance, hypiping.Piping, 0);
				this.TeeFittingSetting(familyInstance, hypiping2.Piping, 1);
				this.TeeFittingSetting(familyInstance, hypiping3.Piping, 2);
				string text = "";
				if (!this.RotateFittingToMainPipe(closeConnector, teeMainConnector, familyInstance, intersectionPoint, out text))
				{
					connMSG = text;
					return null;
				}
				if (!this.RotateFittingToSidePipe(closeConnector, closeConnector3, teeSideConnector, familyInstance, intersectionPoint, out text))
				{
					connMSG = text;
					return null;
				}
				closeConnector.ConnectTo(teeMainConnector);
				closeConnector2.ConnectTo(teeOutConnector);
				closeConnector3.ConnectTo(teeSideConnector);
				YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
				hyrotateFamilyInstance.MovePiping(hypiping.Piping);
				hyrotateFamilyInstance.MovePiping(hypiping3.Piping);
				hyrotateFamilyInstance.MovePiping(hypiping2.Piping);
				document.Regenerate();
			}
			catch (Exception ex)
			{
				familyInstance = null;
				throw ex;
			}
			return familyInstance;
		}

		public FamilyInstance TeeFittingConnPipe(ref IList<Pipe> pipes, string fsName, ref string connMSG)
		{
			FamilyInstance familyInstance = null;
			try
			{
				if (pipes.Count != 3)
				{
					connMSG = "必须是三根水管！";
					return familyInstance;
				}
				List<Pipe> list = new List<Pipe>();
				Document document = pipes[0].Document;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
				YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipes[2]);
				XYZ origin = hypiping.StartConnector.Origin;
				XYZ origin2 = hypiping.EndConnector.Origin;
				new YJKLine(origin, origin2);
				XYZ origin3 = hypiping3.StartConnector.Origin;
				XYZ origin4 = hypiping3.EndConnector.Origin;
				YJKLineEx.YJKGetBound(hypiping3.StartConnector.Origin, hypiping3.EndConnector.Origin);
				if (!hypiping.IsPipingOnLine(hypiping2.Piping))
				{
					connMSG = "第二根水管与第一根水管须在同一条直线上！";
					return familyInstance;
				}
				if (hypiping.Piping.UniqueId == hypiping3.Piping.UniqueId)
				{
					connMSG = "第三根水管与第一根水管不能为同一跟水管！";
					return familyInstance;
				}
				XYZ xyz = hypiping.IntersectionPoint(hypiping3.Piping, 0.016404199475065617);
				if (xyz == null)
				{
					connMSG = "第三根水管与第一根水管没有交点，无法进行连接！";
					return familyInstance;
				}
				YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
				YJKPiping<Pipe> hypiping4;
				if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
				{
					if (!hypiping.IsPointOnPipingCurve(xyz))
					{
						connMSG = "选择的水管不能构成三通连接关系！";
						return familyInstance;
					}
					new List<Pipe>();
					List<Pipe> list2 = hypiping2.Separate(xyz);
					hypiping = new YJKPiping<Pipe>(((IList<Pipe>)list2)[0]);
					hypiping4 = new YJKPiping<Pipe>(((IList<Pipe>)list2)[1]);
					hyrotateFamilyInstance.MovePiping(hypiping.Piping);
					hyrotateFamilyInstance.MovePiping(hypiping4.Piping);
				}
				else
				{
					hypiping4 = new YJKPiping<Pipe>(hypiping2.Piping);
				}
				Connector closeConnector = hypiping.GetCloseConnector(xyz, false);
				hypiping.GetDistanceConnector(xyz);
				Connector closeConnector2 = hypiping4.GetCloseConnector(xyz, false);
				hypiping4.GetDistanceConnector(xyz);
				Connector closeConnector3 = hypiping3.GetCloseConnector(xyz, false);
				hypiping3.GetDistanceConnector(xyz);
				if (closeConnector.IsConnected || closeConnector2.IsConnected || closeConnector3.IsConnected)
				{
					connMSG = "水管已连接！";
					return null;
				}
				if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * closeConnector2.CoordinateSystem.BasisZ, 0.005))
				{
					connMSG = "第二根水管位置不正确！";
					return familyInstance;
				}
				ConnectPipeManage connectPipeManage = new ConnectPipeManage();
				if (string.IsNullOrEmpty(fsName))
				{
					this.NewSomeConnect(new List<Pipe>
					{
						hypiping.Piping,
						hypiping3.Piping,
						hypiping4.Piping
					});
					return null;
				}
				FamilySymbol familySymbolByName = connectPipeManage.GetFamilySymbolByName(fsName, document, ref connMSG);
				if (familySymbolByName == null)
				{
					return familyInstance;
				}
				UnifiedModified.ActivateFamilySymbol(familySymbolByName);
				familyInstance = document.Create.NewFamilyInstance(xyz, familySymbolByName, (Autodesk.Revit.DB.Structure.StructuralType)0);
				document.Regenerate();
				double angle = Math.PI - closeConnector.CoordinateSystem.BasisZ.AngleTo(closeConnector3.CoordinateSystem.BasisZ);
				ConnectHelper connectHelper = new ConnectHelper();
				Connector teeMainConnector = connectHelper.GetTeeMainConnector(familyInstance);
				Connector teeOutConnector = connectHelper.GetTeeOutConnector(familyInstance, teeMainConnector.CoordinateSystem.BasisZ);
				Connector teeSideConnector = connectHelper.GetTeeSideConnector(familyInstance, teeMainConnector.CoordinateSystem.BasisZ);
				this.BranchPipeAngleSet(familyInstance, angle);
				double diameter = hypiping.GetDiameter();
				double diameter2 = hypiping2.GetDiameter();
				Pipe pipe = (diameter > diameter2) ? hypiping.Piping : hypiping2.Piping;
				this.SettingTeeFitting(familyInstance, pipe, 0);
				this.SettingTeeFitting(familyInstance, pipe, 1);
				this.SettingTeeFitting(familyInstance, hypiping3.Piping, 2);
				hyrotateFamilyInstance.MoveFamilyInstance(familyInstance, teeMainConnector.CoordinateSystem.BasisZ);
				document.Regenerate();
				if (closeConnector == null || teeMainConnector == null)
				{
					connMSG = "没有找到连接件！";
					return familyInstance;
				}
				XYZ xyz2;
				Line line;
				if (!this.IsSameConnector(closeConnector, teeMainConnector))
				{
					double num = closeConnector.CoordinateSystem.BasisZ.AngleTo(teeMainConnector.CoordinateSystem.BasisZ);
					if (num == 0.0 || num == Math.PI)
					{
						xyz2 = new XYZ(0.0, 0.0, 1.0);
					}
					else
					{
						xyz2 = closeConnector.CoordinateSystem.BasisZ.CrossProduct(teeMainConnector.CoordinateSystem.BasisZ);
					}
					line = YJKLineEx.YJKGetBound(xyz, xyz + xyz2);
					double num2 = YJKRevitTools.GetTwoVectorAngle(closeConnector.CoordinateSystem.BasisZ, -1.0 * teeMainConnector.CoordinateSystem.BasisZ, xyz2);
					if (num2 > 0.0001)
					{
						ElementTransformUtils.RotateElement(document, familyInstance.Id, line, num2);
						document.Regenerate();
						if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-teeMainConnector.CoordinateSystem.BasisZ))
						{
							ElementTransformUtils.RotateElement(document, familyInstance.Id, line, Math.PI*2 - 2.0 * num2);
							document.Regenerate();
						}
					}
				}
				xyz2 = teeOutConnector.CoordinateSystem.BasisZ;
				line = YJKLineEx.YJKGetBound(xyz, xyz + xyz2);
				int num3 = 0;
				while (!this.IsSameConnector(closeConnector3, teeSideConnector, 0.008))
				{
					double num2 = this.GetAngle(teeOutConnector.CoordinateSystem.BasisZ, teeSideConnector.CoordinateSystem.BasisZ, -1.0 * closeConnector3.CoordinateSystem.BasisZ);
					hyrotateFamilyInstance.RotateElem(familyInstance, xyz, xyz2, num2 * 180.0 / Math.PI);
					document.Regenerate();
					num3++;
					if (num3 > 10)
					{
						break;
					}
				}
				closeConnector.ConnectTo(teeMainConnector);
				connectPipeManage.SetPipeReduceFitting(new List<Pipe>
				{
					hypiping.Piping,
					hypiping4.Piping
				});
				if (closeConnector2.Radius == teeOutConnector.Radius)
				{
					closeConnector2.ConnectTo(teeOutConnector);
				}
				else
				{
					Pipe elem = null;
					Connector connector = this.CreatLittlePipe(hypiping.Piping, teeOutConnector, ref elem);
					FamilyInstance fi = document.Create.NewTransitionFitting(connector, closeConnector2);
					document.DeleteElement(elem);
					document.Regenerate();
					Connector notConnectedConn = this.GetNotConnectedConn(familyInstance, closeConnector2);
					Connector notConnectedConn2 = this.GetNotConnectedConn(fi, teeOutConnector);
					if (notConnectedConn != null && notConnectedConn2 != null)
					{
						notConnectedConn.ConnectTo(notConnectedConn2);
					}
				}
				closeConnector3.ConnectTo(teeSideConnector);
				hyrotateFamilyInstance.MovePiping(hypiping.Piping);
				hyrotateFamilyInstance.MovePiping(hypiping3.Piping);
				hyrotateFamilyInstance.MovePiping(hypiping4.Piping);
				document.Regenerate();
				list.Add(hypiping.Piping);
				list.Add(hypiping4.Piping);
				list.Add(hypiping3.Piping);
				pipes = list;
			}
			catch (Exception ex)
			{
				familyInstance = null;
				throw ex;
			}
			return familyInstance;
		}

		public FamilyInstance TeeFittingConnPipe(IList<Pipe> pipes, string fsName, ref string connMSG)
		{
			FamilyInstance familyInstance = null;
			try
			{
				if (pipes.Count != 3)
				{
					connMSG = "必须是三根水管！";
					return familyInstance;
				}
				Document document = pipes[0].Document;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
				YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipes[2]);
				XYZ origin = hypiping.StartConnector.Origin;
				XYZ origin2 = hypiping.EndConnector.Origin;
				new YJKLine(origin, origin2);
				XYZ origin3 = hypiping3.StartConnector.Origin;
				XYZ origin4 = hypiping3.EndConnector.Origin;
				YJKLineEx.YJKGetBound(hypiping3.StartConnector.Origin, hypiping3.EndConnector.Origin);
				if (!hypiping.IsPipingOnLine(hypiping2.Piping))
				{
					connMSG = "第二根水管与第一根水管须在同一条直线上！";
					return familyInstance;
				}
				if (hypiping.Piping.UniqueId == hypiping3.Piping.UniqueId)
				{
					connMSG = "第三根水管与第一根水管不能为同一跟水管！";
					return familyInstance;
				}
				XYZ xyz = hypiping.IntersectionPoint(hypiping3.Piping, 0.016404199475065617);
				if (xyz == null)
				{
					connMSG = "第三根水管与第一根水管没有交点，无法进行连接！";
					return familyInstance;
				}
				YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
				YJKPiping<Pipe> hypiping4;
				if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
				{
					if (!hypiping.IsPointOnPipingCurve(xyz))
					{
						connMSG = "选择的水管不能构成三通连接关系！";
						return familyInstance;
					}
					new List<Pipe>();
					List<Pipe> list = hypiping2.Separate(xyz);
					hypiping = new YJKPiping<Pipe>(((IList<Pipe>)list)[0]);
					hypiping4 = new YJKPiping<Pipe>(((IList<Pipe>)list)[1]);
					hyrotateFamilyInstance.MovePiping(hypiping.Piping);
					hyrotateFamilyInstance.MovePiping(hypiping4.Piping);
				}
				else
				{
					hypiping4 = new YJKPiping<Pipe>(hypiping2.Piping);
				}
				Connector closeConnector = hypiping.GetCloseConnector(xyz, false);
				hypiping.GetDistanceConnector(xyz);
				Connector closeConnector2 = hypiping4.GetCloseConnector(xyz, false);
				hypiping4.GetDistanceConnector(xyz);
				Connector closeConnector3 = hypiping3.GetCloseConnector(xyz, false);
				hypiping3.GetDistanceConnector(xyz);
				if (closeConnector.IsConnected || closeConnector2.IsConnected || closeConnector3.IsConnected)
				{
					connMSG = "水管已连接！";
					return null;
				}
				if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * closeConnector2.CoordinateSystem.BasisZ, 0.005))
				{
					connMSG = "第二根水管位置不正确！";
					return familyInstance;
				}
				ConnectPipeManage connectPipeManage = new ConnectPipeManage();
				if (string.IsNullOrEmpty(fsName))
				{
					this.NewSomeConnect(new List<Pipe>
					{
						hypiping.Piping,
						hypiping3.Piping,
						hypiping4.Piping
					});
					return null;
				}
				FamilySymbol familySymbolByName = connectPipeManage.GetFamilySymbolByName(fsName, document, ref connMSG);
				if (familySymbolByName == null)
				{
					return familyInstance;
				}
				UnifiedModified.ActivateFamilySymbol(familySymbolByName);
				familyInstance = document.Create.NewFamilyInstance(xyz, familySymbolByName, (Autodesk.Revit.DB.Structure.StructuralType)0);
				document.Regenerate();
				double angle = Math.PI - closeConnector.CoordinateSystem.BasisZ.AngleTo(closeConnector3.CoordinateSystem.BasisZ);
				ConnectHelper connectHelper = new ConnectHelper();
				Connector teeMainConnector = connectHelper.GetTeeMainConnector(familyInstance);
				Connector teeOutConnector = connectHelper.GetTeeOutConnector(familyInstance, teeMainConnector.CoordinateSystem.BasisZ);
				Connector teeSideConnector = connectHelper.GetTeeSideConnector(familyInstance, teeMainConnector.CoordinateSystem.BasisZ);
				this.BranchPipeAngleSet(familyInstance, angle);
				double diameter = hypiping.GetDiameter();
				double diameter2 = hypiping2.GetDiameter();
				Pipe pipe = (diameter > diameter2) ? hypiping.Piping : hypiping2.Piping;
				this.SettingTeeFitting(familyInstance, pipe, 0);
				this.SettingTeeFitting(familyInstance, pipe, 1);
				this.SettingTeeFitting(familyInstance, hypiping3.Piping, 2);
				hyrotateFamilyInstance.MoveFamilyInstance(familyInstance, teeMainConnector.CoordinateSystem.BasisZ);
				document.Regenerate();
				if (closeConnector == null || teeMainConnector == null)
				{
					connMSG = "没有找到连接件！";
					return familyInstance;
				}
				XYZ xyz2;
				Line line;
				if (!this.IsSameConnector(closeConnector, teeMainConnector))
				{
					double num = closeConnector.CoordinateSystem.BasisZ.AngleTo(teeMainConnector.CoordinateSystem.BasisZ);
					if (num == 0.0 || num == Math.PI)
					{
						xyz2 = new XYZ(0.0, 0.0, 1.0);
					}
					else
					{
						xyz2 = closeConnector.CoordinateSystem.BasisZ.CrossProduct(teeMainConnector.CoordinateSystem.BasisZ);
					}
					line = YJKLineEx.YJKGetBound(xyz, xyz + xyz2);
					double num2 = YJKRevitTools.GetTwoVectorAngle(closeConnector.CoordinateSystem.BasisZ, -1.0 * teeMainConnector.CoordinateSystem.BasisZ, xyz2);
					if (num2 > 0.0001)
					{
						ElementTransformUtils.RotateElement(document, familyInstance.Id, line, num2);
						document.Regenerate();
						if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-teeMainConnector.CoordinateSystem.BasisZ))
						{
							ElementTransformUtils.RotateElement(document, familyInstance.Id, line, Math.PI*2 - 2.0 * num2);
							document.Regenerate();
						}
					}
				}
				xyz2 = teeOutConnector.CoordinateSystem.BasisZ;
				line = YJKLineEx.YJKGetBound(xyz, xyz + xyz2);
				int num3 = 0;
				while (!this.IsSameConnector(closeConnector3, teeSideConnector, 0.008))
				{
					double num2 = this.GetAngle(teeOutConnector.CoordinateSystem.BasisZ, teeSideConnector.CoordinateSystem.BasisZ, -1.0 * closeConnector3.CoordinateSystem.BasisZ);
					hyrotateFamilyInstance.RotateElem(familyInstance, xyz, xyz2, num2 * 180.0 / Math.PI);
					document.Regenerate();
					num3++;
					if (num3 > 10)
					{
						break;
					}
				}
				closeConnector.ConnectTo(teeMainConnector);
				connectPipeManage.SetPipeReduceFitting(new List<Pipe>
				{
					hypiping.Piping,
					hypiping4.Piping
				});
				if (closeConnector2.Radius == teeOutConnector.Radius)
				{
					closeConnector2.ConnectTo(teeOutConnector);
				}
				else
				{
					Pipe elem = null;
					Connector connector = this.CreatLittlePipe(hypiping.Piping, teeOutConnector, ref elem);
					FamilyInstance fi = document.Create.NewTransitionFitting(connector, closeConnector2);
					document.DeleteElement(elem);
					document.Regenerate();
					Connector notConnectedConn = this.GetNotConnectedConn(familyInstance, closeConnector2);
					Connector notConnectedConn2 = this.GetNotConnectedConn(fi, teeOutConnector);
					if (notConnectedConn != null && notConnectedConn2 != null)
					{
						notConnectedConn.ConnectTo(notConnectedConn2);
					}
				}
				closeConnector3.ConnectTo(teeSideConnector);
				hyrotateFamilyInstance.MovePiping(hypiping.Piping);
				hyrotateFamilyInstance.MovePiping(hypiping3.Piping);
				hyrotateFamilyInstance.MovePiping(hypiping4.Piping);
				document.Regenerate();
			}
			catch (Exception ex)
			{
				familyInstance = null;
				throw ex;
			}
			return familyInstance;
		}

		private double GetAngle(XYZ baseVector, XYZ teeVector, XYZ pipeVector)
		{
			double twoVectorAngle = YJKRevitTools.GetTwoVectorAngle(baseVector, teeVector, baseVector.CrossProduct(teeVector));
			XYZ xyz = 1.0 / Math.Cos(twoVectorAngle) * teeVector - baseVector;
			YJKRevitTools.GetTwoVectorAngle(baseVector, pipeVector, baseVector.CrossProduct(pipeVector));
			XYZ xyz2 = 1.0 / Math.Cos(twoVectorAngle) * pipeVector - baseVector;
			return YJKRevitTools.GetTwoVectorAngle(xyz, xyz2, xyz.CrossProduct(xyz2));
		}

		public FamilyInstance NewCrossFitting(IList<Pipe> pipes, IList<XYZ> xyzs, string crossName, ref string connMSG)
		{
			FamilyInstance familyInstance = null;
			SubTransaction subTransaction = null;
			FamilyInstance result;
			try
			{
				if (pipes.Count != 4)
				{
					connMSG = "四通连接，必须是四根水管！";
					result = null;
				}
				else
				{
					Document document = pipes[0].Document;
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0], xyzs[0]);
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1], xyzs[1]);
					YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipes[2], xyzs[2]);
					YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipes[3], xyzs[3]);
					YJKPiping<Pipe> hypiping5 = null;
					YJKPiping<Pipe> hypiping6 = null;
					subTransaction = new SubTransaction(document);
					subTransaction.Start();
					if (hypiping.Piping.UniqueId == hypiping3.Piping.UniqueId)
					{
						connMSG = "第三根水管与第一根水管不能为同一跟水管！";
						result = familyInstance;
					}
					else if (hypiping.Piping.UniqueId == hypiping4.Piping.UniqueId)
					{
						connMSG = "第四根水管与第一根水管不能为同一跟水管！";
						result = familyInstance;
					}
					else
					{
						YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
						new YJKLine(hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
						YJKLine hyline2 = new YJKLine(hypiping3.StartConnector.Origin, hypiping3.EndConnector.Origin);
						YJKLine hyline3 = new YJKLine(hypiping4.StartConnector.Origin, hypiping4.EndConnector.Origin);
						if (!hyline.IsPointOnCurveLine(hypiping2.StartConnector.Origin) || !hyline.IsPointOnCurveLine(hypiping2.EndConnector.Origin))
						{
							connMSG = "选择的第二条水管必须与第一条水管在同一直线上！";
							result = familyInstance;
						}
						else if (!hyline2.IsPointOnCurveLine(hypiping4.StartConnector.Origin) || !hyline2.IsPointOnCurveLine(hypiping4.EndConnector.Origin))
						{
							connMSG = "选择的第四条水管必须与第三条水管在同一直线上！";
							result = familyInstance;
						}
						else if (!hyline.IsVertical(hyline2.Line))
						{
							connMSG = "选择的第三条水管必须与第一条水管垂直！";
							result = familyInstance;
						}
						else if (!hyline.IsVertical(hyline3.Line))
						{
							connMSG = "选择的第四条水管必须与第一条水管垂直！";
							result = familyInstance;
						}
						else if (hypiping.IntersectionPoint(hypiping3.Piping, 0.016404199475065617) == null || hypiping.IntersectionPoint(hypiping4.Piping, 0.00984251968503937) == null || hypiping2.IntersectionPoint(hypiping3.Piping, 0.016404199475065617) == null || hypiping2.IntersectionPoint(hypiping4.Piping, 0.00984251968503937) == null)
						{
							connMSG = "选择的四条水管，没有共同的交点，无法进行连接！";
							result = familyInstance;
						}
						else if (string.IsNullOrEmpty(crossName))
						{
							IList<Pipe> list = new List<Pipe>();
							list.Add(pipes[0]);
							if (pipes[0].Id != pipes[1].Id)
							{
								list.Add(pipes[1]);
							}
							list.Add(pipes[2]);
							if (pipes[2].Id != pipes[3].Id)
							{
								list.Add(pipes[3]);
							}
							this.NewSomeConnect(list);
							subTransaction.Commit();
							result = null;
						}
						else
						{
							XYZ xyz = hypiping.IntersectionPoint(hypiping3.Piping, 0.016404199475065617);
							if (xyz == null)
							{
								connMSG = "选择的四条水管，没有共同的交点，无法进行连接！";
								result = familyInstance;
							}
							else
							{
								YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
								if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
								{
									if (!hypiping.IsPointOnPipingCurve(xyz))
									{
										connMSG = "选择的四条水管，没有共同的交点，无法进行连接！";
										return familyInstance;
									}
									IList<Pipe> list2 = new List<Pipe>();
									list2 = hypiping.Separate(xyz);
									hypiping5 = new YJKPiping<Pipe>(list2[1], xyzs[1]);
									hyrotateFamilyInstance.MovePiping(hypiping.Piping);
									hyrotateFamilyInstance.MovePiping(hypiping5.Piping);
									IList<Pipe> orderDucts = this.GetOrderDucts(list2, xyzs[0]);
									hypiping = new YJKPiping<Pipe>(orderDucts[0]);
									hypiping5 = new YJKPiping<Pipe>(orderDucts[1]);
								}
								else if (hypiping.IsPipingOnLine(hypiping2.Piping))
								{
									hypiping5 = new YJKPiping<Pipe>(hypiping2.Piping, xyzs[1]);
								}
								if (hypiping3.Piping.UniqueId == hypiping4.Piping.UniqueId)
								{
									if (!hypiping3.IsPointOnPipingCurve(xyz))
									{
										connMSG = "选择的四条水管，没有共同的交点，无法进行连接！";
										return familyInstance;
									}
									IList<Pipe> list3 = new List<Pipe>();
									list3 = hypiping3.Separate(xyz);
									hypiping6 = new YJKPiping<Pipe>(list3[1], xyzs[3]);
									hyrotateFamilyInstance.MovePiping(hypiping3.Piping);
									hyrotateFamilyInstance.MovePiping(hypiping6.Piping);
									IList<Pipe> orderDucts2 = this.GetOrderDucts(list3, xyzs[2]);
									hypiping3 = new YJKPiping<Pipe>(orderDucts2[0]);
									hypiping6 = new YJKPiping<Pipe>(orderDucts2[1]);
								}
								else if (hypiping3.IsPipingOnLine(hypiping4.Piping))
								{
									hypiping6 = new YJKPiping<Pipe>(hypiping4.Piping, xyzs[3]);
								}
								if (hypiping5 == null || hypiping6 == null)
								{
									connMSG = "分割水管失败！";
									result = familyInstance;
								}
								else
								{
									Connector appropriateConnector = hypiping.GetAppropriateConnector(xyz);
									Connector appropriateConnector2 = hypiping5.GetAppropriateConnector(xyz);
									Connector appropriateConnector3 = hypiping3.GetAppropriateConnector(xyz);
									Connector appropriateConnector4 = hypiping6.GetAppropriateConnector(xyz);
									double diameter = hypiping.GetDiameter();
									double diameter2 = hypiping5.GetDiameter();
									double diameter3 = hypiping3.GetDiameter();
									double diameter4 = hypiping6.GetDiameter();
									Pipe pipe = (diameter > diameter2) ? hypiping.Piping : hypiping5.Piping;
									Pipe pipe2 = (diameter3 > diameter4) ? hypiping3.Piping : hypiping6.Piping;
									if (appropriateConnector == null || appropriateConnector2 == null || appropriateConnector3 == null || appropriateConnector4 == null)
									{
										connMSG = "没有找到连接口！";
										result = familyInstance;
									}
									else if (appropriateConnector.IsConnected || appropriateConnector2.IsConnected || appropriateConnector3.IsConnected || appropriateConnector4.IsConnected)
									{
										connMSG = "水管已连接！";
										result = familyInstance;
									}
									else
									{
										ConnectPipeManage connectPipeManage = new ConnectPipeManage();
										FamilySymbol familySymbolByName = connectPipeManage.GetFamilySymbolByName(crossName, document, ref connMSG);
										if (familySymbolByName == null)
										{
											result = familyInstance;
										}
										else
										{
											UnifiedModified.ActivateFamilySymbol(familySymbolByName);
											FamilyInstance familyInstance2 = document.Create.NewFamilyInstance(xyz, familySymbolByName, (Autodesk.Revit.DB.Structure.StructuralType)0);
											document.Regenerate();
											ConnectHelper connectHelper = new ConnectHelper();
											Connector teeMainConnector = connectHelper.GetTeeMainConnector(familyInstance2);
											Connector teeOutConnector = connectHelper.GetTeeOutConnector(familyInstance2, teeMainConnector.CoordinateSystem.BasisZ);
											Connector teeOutConnector2 = connectHelper.GetTeeOutConnector(familyInstance2, -1.0 * new XYZ(0.0, 1.0, 0.0));
											Connector teeOutConnector3 = connectHelper.GetTeeOutConnector(familyInstance2, new XYZ(0.0, 1.0, 0.0));
											this.SettingTeeFitting(familyInstance2, pipe, 0);
											this.SettingTeeFitting(familyInstance2, pipe, 1);
											this.SettingTeeFitting(familyInstance2, pipe2, 3);
											this.SettingTeeFitting(familyInstance2, pipe2, 4);
											hyrotateFamilyInstance.MoveFamilyInstance(familyInstance2, teeMainConnector.CoordinateSystem.BasisZ);
											XYZ xyz2;
											Line line;
											if (!this.IsSameConnector(appropriateConnector, teeMainConnector))
											{
												double num = appropriateConnector.CoordinateSystem.BasisZ.AngleTo(teeMainConnector.CoordinateSystem.BasisZ);
												if (num == 0.0 || num == Math.PI)
												{
													xyz2 = new XYZ(0.0, 0.0, 1.0);
												}
												else
												{
													xyz2 = appropriateConnector.CoordinateSystem.BasisZ.CrossProduct(-1.0 * teeMainConnector.CoordinateSystem.BasisZ);
												}
												line = YJKLineEx.YJKGetBound(xyz, xyz + xyz2 * 3.0);
												double num2 = YJKRevitTools.GetTwoVectorAngle(appropriateConnector.CoordinateSystem.BasisZ, -1.0 * teeMainConnector.CoordinateSystem.BasisZ, xyz2);
												if (num2 > 0.0001)
												{
													ElementTransformUtils.RotateElement(document, familyInstance2.Id, line, num2);
													document.Regenerate();
													if (!appropriateConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-teeMainConnector.CoordinateSystem.BasisZ))
													{
														ElementTransformUtils.RotateElement(document, familyInstance2.Id, line, Math.PI*2 - 2.0 * num2);
														document.Regenerate();
													}
												}
											}
											xyz2 = appropriateConnector.CoordinateSystem.BasisZ;
											line = YJKLineEx.YJKGetBound(xyz, xyz + xyz2 * 3.0);
											int num3 = 0;
											while (!this.IsSameConnector(appropriateConnector3, teeOutConnector2, 0.03))
											{
												double num2 = YJKRevitTools.GetTwoXYVectorAngle(appropriateConnector3.CoordinateSystem.BasisZ, -1.0 * teeOutConnector2.CoordinateSystem.BasisZ) * 180.0 / Math.PI;
												if (xyz2.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0)))
												{
													num2 = 180.0;
												}
												hyrotateFamilyInstance.RotateElem(familyInstance2, xyz, xyz2, num2);
												document.Regenerate();
												num3++;
												if (num3 > 10)
												{
													break;
												}
											}
											connectPipeManage.SetPipeReduceFitting(new List<Pipe>
											{
												hypiping.Piping,
												hypiping3.Piping,
												hypiping5.Piping,
												hypiping6.Piping
											});
											appropriateConnector.ConnectTo(teeMainConnector);
											if (appropriateConnector2.Radius == teeOutConnector.Radius)
											{
												appropriateConnector2.ConnectTo(teeOutConnector);
											}
											else
											{
												Pipe elem = null;
												Connector connector = this.CreatLittlePipe(hypiping.Piping, teeOutConnector, ref elem);
												FamilyInstance fi = document.Create.NewTransitionFitting(connector, appropriateConnector2);
												document.DeleteElement(elem);
												document.Regenerate();
												Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, appropriateConnector2);
												Connector notConnectedConn2 = this.GetNotConnectedConn(fi, teeOutConnector);
												if (notConnectedConn != null && notConnectedConn2 != null)
												{
													notConnectedConn.ConnectTo(notConnectedConn2);
												}
											}
											appropriateConnector3.ConnectTo(teeOutConnector2);
											if (appropriateConnector4.Radius == teeOutConnector3.Radius)
											{
												appropriateConnector4.ConnectTo(teeOutConnector3);
											}
											else
											{
												Pipe elem2 = null;
												Connector connector = this.CreatLittlePipe(hypiping3.Piping, teeOutConnector3, ref elem2);
												FamilyInstance fi = document.Create.NewTransitionFitting(connector, appropriateConnector4);
												document.DeleteElement(elem2);
												document.Regenerate();
												Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, appropriateConnector4);
												Connector notConnectedConn2 = this.GetNotConnectedConn(fi, teeOutConnector3);
												if (notConnectedConn != null && notConnectedConn2 != null)
												{
													notConnectedConn.ConnectTo(notConnectedConn2);
												}
											}
											hyrotateFamilyInstance.MovePiping(hypiping.Piping);
											hyrotateFamilyInstance.MovePiping(hypiping5.Piping);
											hyrotateFamilyInstance.MovePiping(hypiping3.Piping);
											hyrotateFamilyInstance.MovePiping(hypiping6.Piping);
											familyInstance = familyInstance2;
											subTransaction.Commit();
											result = familyInstance;
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private IList<Pipe> GetOrderDucts(IList<Pipe> listDuct, XYZ pickPoint)
		{
			IList<Pipe> list = new List<Pipe>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(listDuct[0]);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(listDuct[1]);
			double num = hypiping.GetProject(pickPoint).DistanceTo(pickPoint);
			double num2 = hypiping2.GetProject(pickPoint).DistanceTo(pickPoint);
			if (num > num2)
			{
				list.Add(listDuct[1]);
				list.Add(listDuct[0]);
			}
			else
			{
				list.Add(listDuct[0]);
				list.Add(listDuct[1]);
			}
			return list;
		}

		private Connector CreatLittlePipe(Pipe pipe, Connector conn, ref Pipe newPipe)
		{
			XYZ origin = conn.Origin;
			XYZ basisZ = conn.CoordinateSystem.BasisZ;
			Pipe pipe2 = YJKRevitTools.NewPipe(pipe, origin, origin + 0.016404199475065617 * basisZ);
			pipe.Document.Regenerate();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe2);
			new YJKRotateFamilyInstance();
			Connector connector;
			Connector connector2;
			if (conn.CoordinateSystem.BasisZ.IsAlmostEqualTo(hypiping.StartConnector.Connector.CoordinateSystem.BasisZ, 0.0001))
			{
				connector = hypiping.EndConnector.Connector;
				connector2 = hypiping.StartConnector.Connector;
			}
			else
			{
				connector2 = hypiping.EndConnector.Connector;
				connector = hypiping.StartConnector.Connector;
			}
			connector.ConnectTo(conn);
			newPipe = pipe2;
			return connector2;
		}

		private Connector GetNotConnectedConn(FamilyInstance fi, Connector connector)
		{
			Connector result = null;
			if (fi != null)
			{
				ConnectorSetIterator connectorSetIterator = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector2 = obj as Connector;
					if (connector2 != null && !connector2.IsConnected && connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * connector.CoordinateSystem.BasisZ, 0.0005))
					{
						result = connector2;
					}
				}
			}
			return result;
		}

		private FamilyInstance TeeFittingConnSlopePipe(List<YJKPiping<Pipe>> pipes, FamilySymbol fs, XYZ intersectionPoint, ref string connMsg)
		{
			FamilyInstance result = null;
			try
			{
				Document document = pipes[0].Piping.Document;
				YJKPiping<Pipe> hypiping = pipes[0];
				YJKPiping<Pipe> hypiping2 = pipes[1];
				YJKPiping<Pipe> hypiping3 = pipes[2];
				Connector closeConnector = hypiping.GetCloseConnector(intersectionPoint, false);
				Connector closeConnector2 = hypiping2.GetCloseConnector(intersectionPoint, false);
				Connector closeConnector3 = hypiping3.GetCloseConnector(intersectionPoint, false);
				if (closeConnector.IsConnected || closeConnector2.IsConnected || closeConnector3.IsConnected)
				{
					connMsg = "水管已连接！";
					return null;
				}
				hypiping.ChangeFirstTee(fs);
				hypiping2.ChangeFirstTee(fs);
				hypiping3.ChangeFirstTee(fs);
				result = document.Create.NewTeeFitting(closeConnector, closeConnector2, closeConnector3);
				document.Regenerate();
			}
			catch (Exception ex)
			{
				result = null;
				throw ex;
			}
			return result;
		}

		private bool TeeFittingSetting(FamilyInstance fi, Pipe pipe, int num)
		{
			if (fi == null || pipe == null)
			{
				throw new Exception("水管或者设定的管件不能为空！");
			}
			double num2 = pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
			string text = "";
			switch (num)
			{
			case 0:
				text = "入口直径";
				break;
			case 1:
				text = "出口直径";
				break;
			case 2:
				text = "支管直径";
				break;
			}
			Parameter parameter = fi.GetParameter(text);
			if (parameter == null)
			{
				throw new Exception("三通族上不存在*" + text + "*的参数");
			}
			if (parameter.IsReadOnly)
			{
				throw new Exception("三通族上*" + text + "*的参数为只读属性，不能修改！");
			}
			parameter.Set(num2);
			return true;
		}

		private int SettingTeeFitting(FamilyInstance fi, Pipe pipe, int num)
		{
			if (fi == null || pipe == null)
			{
				return -1;
			}
			double num2 = pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
			string paramName = "";
			switch (num)
			{
			case 0:
				paramName = "入口直径";
				break;
			case 1:
				paramName = "出口直径";
				break;
			case 2:
				paramName = "支管直径";
				break;
			case 3:
				paramName = "支管1直径";
				break;
			case 4:
				paramName = "支管2直径";
				break;
			}
			Parameter parameter = fi.GetParameter(paramName);
			if (parameter == null)
			{
				return -2;
			}
			if (parameter.IsReadOnly)
			{
				return -3;
			}
			if (parameter.Set(num2))
			{
				return 1;
			}
			return 0;
		}

		private bool ReduceFittingSetting(FamilyInstance fi, Pipe pipe, int num)
		{
			if (fi == null || pipe == null)
			{
				throw new Exception("水管或者设定的管件不能为空！");
			}
			double num2 = pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() / 2.0;
			string text = "";
			if (num != 0)
			{
				if (num == 1)
				{
					text = "偏移宽度";
				}
			}
			else
			{
				text = "偏移高度";
			}
			Parameter parameter = fi.GetParameter(text);
			if (parameter == null)
			{
				throw new Exception("变径族上不存在*" + text + "*的参数");
			}
			if (parameter.IsReadOnly)
			{
				throw new Exception("变径族上*" + text + "*的参数为只读属性，不能修改！");
			}
			parameter.Set(num2);
			return true;
		}

		private bool RotateFittingToMainPipe(Connector pipeConn, Connector fitConn, FamilyInstance fi, XYZ intersectionPoint, out string rotateMSG)
		{
			rotateMSG = "";
			if (pipeConn == null || fitConn == null)
			{
				rotateMSG = "连接件不能为空！";
				return false;
			}
			if (this.IsSameConnector(pipeConn, fitConn))
			{
				return true;
			}
			XYZ xyz = new XYZ(0.0, 0.0, 1.0);
			double degree = 360.0 - YJKRevitTools.GetTwoVectorAngle(pipeConn.CoordinateSystem.BasisZ, -fitConn.CoordinateSystem.BasisZ, xyz) * 180.0 / Math.PI;
			new YJKRotateFamilyInstance().RotateElem(fi, intersectionPoint, xyz, degree);
			fi.Document.Regenerate();
			return true;
		}

		private bool RotateFittingToSidePipe(Connector pipeInConn, Connector pipeConn, Connector fitConn, FamilyInstance fi, XYZ intersectionPoint, out string rotateMSG)
		{
			rotateMSG = "";
			if (pipeConn == null || fitConn == null)
			{
				rotateMSG = "连接件不能为空！";
				return false;
			}
			XYZ vector = pipeInConn.CoordinateSystem.BasisZ.Normalize();
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			int num = 0;
			while (!this.IsSameConnector(pipeConn, fitConn))
			{
				hyrotateFamilyInstance.RotateElem(fi, pipeInConn.Origin, vector, 180.0);
				fi.Document.Regenerate();
				if (num > 20)
				{
					break;
				}
				num++;
			}
			return true;
		}

		private bool ConnFittingPipe(FamilyInstance fi, Connector pipeConn)
		{
			YJKMessageBox.Show(pipeConn.Origin.ToString());
			Connector connector = null;
			if (fi != null)
			{
				ConnectorSetIterator connectorSetIterator = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector2 = obj as Connector;
					if (connector2 != null && connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(-pipeConn.CoordinateSystem.BasisZ))
					{
						connector = connector2;
					}
				}
			}
			connector.ConnectTo(pipeConn);
			return true;
		}

		private bool BranchPipeAngleSet(FamilyInstance fi, XYZ outVector, XYZ sideVector)
		{
			Parameter parameter = fi.GetParameter("支管角度");
			if (parameter == null)
			{
				throw new Exception("三通族上不存在*支管角度*的参数");
			}
			if (parameter.IsReadOnly)
			{
				throw new Exception("三通族上*支管角度*的参数为只读属性，不能修改！");
			}
			double num = 0.0;
			if (!outVector.IsAlmostEqualTo(sideVector))
			{
				num = outVector.AngleTo(sideVector);
			}
			parameter.Set(num);
			return true;
		}

		private int BranchPipeAngleSet(FamilyInstance fi, double angle)
		{
			bool flag = false;
			Parameter parameter = fi.GetParameter("支管角度");
			if (parameter == null)
			{
				return -1;
			}
			if (parameter.IsReadOnly)
			{
				return -2;
			}
			if (angle != 0.0)
			{
				flag = parameter.Set(angle);
			}
			if (flag)
			{
				return 1;
			}
			return 0;
		}

		private IList<Pipe> SortThreePipes(IList<Pipe> pipes, out string orderMsg)
		{
			IList<Pipe> list = new List<Pipe>();
			orderMsg = "";
			if (pipes.Count != 3)
			{
				orderMsg = "必须是三根水管！";
				return null;
			}
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
			YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipes[2]);
			if (hypiping.IsPipingOnLine(hypiping2.Piping) || hypiping.IsXYParallel(hypiping2.Piping))
			{
				list.Add(hypiping.Piping);
				list.Add(hypiping2.Piping);
				list.Add(hypiping3.Piping);
			}
			else if (hypiping.IsPipingOnLine(hypiping3.Piping) || hypiping.IsXYParallel(hypiping3.Piping))
			{
				list.Add(hypiping.Piping);
				list.Add(hypiping3.Piping);
				list.Add(hypiping2.Piping);
			}
			else
			{
				if (!hypiping2.IsPipingOnLine(hypiping3.Piping) && !hypiping2.IsXYParallel(hypiping3.Piping))
				{
					orderMsg = "水管位置不正确，必须有两根水管在同一直线上！";
					return null;
				}
				list.Add(hypiping2.Piping);
				list.Add(hypiping3.Piping);
				list.Add(hypiping.Piping);
			}
			return list;
		}

		private bool IsSameConnector(Connector conn1, Connector conn2)
		{
			bool result = true;
			if (conn1 == null || conn2 == null)
			{
				return false;
			}
			double num = conn1.CoordinateSystem.BasisZ.AngleTo(-conn2.CoordinateSystem.BasisZ);
			if (!conn1.CoordinateSystem.BasisZ.IsAlmostEqualTo(-conn2.CoordinateSystem.BasisZ) || num > 0.005)
			{
				result = false;
			}
			else if (Math.Abs(conn1.Radius - conn2.Radius) > 0.001)
			{
				result = false;
			}
			return result;
		}

		private bool IsSameConnector(Connector conn1, Connector conn2, double deviation)
		{
			bool result = true;
			if (conn1 == null || conn2 == null)
			{
				return false;
			}
			double num = conn1.CoordinateSystem.BasisZ.AngleTo(-conn2.CoordinateSystem.BasisZ);
			if (!conn1.CoordinateSystem.BasisZ.IsAlmostEqualTo(-conn2.CoordinateSystem.BasisZ) || num > deviation)
			{
				result = false;
			}
			return result;
		}

		private void SettingTeeParameter(FamilyInstance teeFi, YJKPiping<Pipe> hyPipe, YJKPiping<Pipe> hyPipeOther, int i)
		{
			IList<Connector> appropriateConnector = hyPipe.GetAppropriateConnector(hyPipeOther.Piping);
			double num = 0.0;
			if (appropriateConnector != null && appropriateConnector.Count > 1)
			{
				if (appropriateConnector[0].CoordinateSystem.BasisZ.IsAlmostEqualTo(-appropriateConnector[1].CoordinateSystem.BasisZ))
				{
					num = 0.0;
				}
				else
				{
					num = appropriateConnector[0].CoordinateSystem.BasisZ.AngleTo(-appropriateConnector[1].CoordinateSystem.BasisZ);
				}
			}
			Parameter parameter = hyPipeOther.Piping.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM);
			if (1 == i)
			{
				if (parameter != null)
				{
					Parameter parameter2 = teeFi.GetParameter("出口直径");
					if (parameter2 != null && !parameter2.IsReadOnly)
					{
						parameter2.Set(parameter.AsDouble());
					}
				}
				if (num > 0.0001)
				{
					Parameter parameter3 = teeFi.GetParameter("出口角度");
					if (parameter3 != null && !parameter3.IsReadOnly)
					{
						parameter3.Set(num);
						return;
					}
					if (Math.Abs(num - Math.PI*.5) > 0.0001)
					{
						throw new Exception("管件没有出口角度或角度为只读，请选择正确的管件");
					}
				}
			}
			else if (2 == i)
			{
				if (parameter != null)
				{
					Parameter parameter4 = teeFi.GetParameter("支管直径");
					if (parameter4 != null && !parameter4.IsReadOnly)
					{
						parameter4.Set(parameter.AsDouble());
					}
				}
				if (num > 0.0001)
				{
					Parameter parameter5 = teeFi.GetParameter("角度");
					if (parameter5 == null || parameter5.AsDouble().ToString() == null)
					{
						parameter5 = teeFi.GetParameter("支管角度");
					}
					if (parameter5 != null && !parameter5.IsReadOnly)
					{
						parameter5.Set(num);
						return;
					}
					if (Math.Abs(num - Math.PI*.5) > 0.0001)
					{
						throw new Exception("管件没有支管角度或角度为只读，请选择正确的管件");
					}
				}
			}
		}

		public bool NewCrossFitting(ref IList<Pipe> pipes, bool beReginerate = true)
		{
			bool flag = false;
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (pipes.Count < 4)
				{
					throw new System.ArgumentNullException("Pipe", "is null");
				}
				Document document = pipes[0].Document;
				new List<Pipe>();
				IList<Pipe> list = this.OrderPipePosition(pipes);
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(list[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(list[1]);
				YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(list[2]);
				YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(list[3]);
				YJKPiping<Pipe> hypiping5 = null;
				YJKPiping<Pipe> hypiping6 = null;
				if (beReginerate)
				{
					subTransaction = new SubTransaction(document);
					subTransaction.Start();
				}
				XYZ xyz = hypiping.IntersectionPoint(hypiping3.Piping, 0.03);
				if (xyz != null)
				{
					if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
					{
						new List<Pipe>();
						hypiping5 = new YJKPiping<Pipe>(((IList<Pipe>)hypiping.Separate(xyz))[1]);
					}
					else if (hypiping.IsPipingOnLine(hypiping2.Piping))
					{
						hypiping5 = new YJKPiping<Pipe>(hypiping2.Piping);
					}
					if (hypiping3.Piping.UniqueId == hypiping4.Piping.UniqueId)
					{
						new List<Pipe>();
						hypiping6 = new YJKPiping<Pipe>(((IList<Pipe>)hypiping3.Separate(xyz))[1]);
					}
					else if (hypiping3.IsPipingOnLine(hypiping4.Piping))
					{
						hypiping6 = new YJKPiping<Pipe>(hypiping4.Piping);
					}
					if (hypiping5 != null && hypiping6 != null)
					{
						Connector connector = null;
						Connector connector2 = null;
						Connector connector3 = null;
						Connector connector4 = null;
						this.GetOrderPipesConnectors(hypiping, hypiping5, xyz, ref connector, ref connector2);
						this.GetOrderPipesConnectors(hypiping3, hypiping6, xyz, ref connector3, ref connector4);
						if (this.GetPipesMaxDiameter(hypiping, hypiping5) < this.GetPipesMaxDiameter(hypiping3, hypiping6))
						{
							this.GetOrderPipesConnectors(hypiping3, hypiping6, xyz, ref connector, ref connector2);
							this.GetOrderPipesConnectors(hypiping, hypiping5, xyz, ref connector3, ref connector4);
						}
						if (connector != null && !connector.IsConnected && connector2 != null && !connector2.IsConnected && connector3 != null && !connector3.IsConnected && connector4 != null && !connector4.IsConnected)
						{
							document.Create.NewCrossFitting(connector, connector2, connector4, connector3);
							flag = true;
						}
					}
					pipes.Clear();
					pipes.Add(hypiping.Piping);
					pipes.Add(hypiping5.Piping);
					pipes.Add(hypiping3.Piping);
					pipes.Add(hypiping6.Piping);
				}
				if (beReginerate)
				{
					document.Regenerate();
					subTransaction.Commit();
				}
				result = flag;
			}
			catch (Exception)
			{
				flag = false;
				result = flag;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		private double GetPipesMaxDiameter(YJKPiping<Pipe> hyPipe1, YJKPiping<Pipe> hyPipe2)
		{
			if (hyPipe1.GetDiameter() <= hyPipe2.GetDiameter())
			{
				return hyPipe2.GetDiameter();
			}
			return hyPipe1.GetDiameter();
		}

		private bool JudgeBeMaxPipe(YJKPiping<Pipe> hyPipe1, YJKPiping<Pipe> hyPipe2)
		{
			return hyPipe1.GetDiameter() > hyPipe2.GetDiameter();
		}

		private void GetOrderPipesConnectors(YJKPiping<Pipe> hyPipe1, YJKPiping<Pipe> hyPipe2, XYZ intersectionPoint, ref Connector conn1, ref Connector conn2)
		{
			if (this.JudgeBeMaxPipe(hyPipe1, hyPipe2))
			{
				conn1 = hyPipe1.GetAppropriateConnector(intersectionPoint);
				conn2 = hyPipe2.GetAppropriateConnector(intersectionPoint);
				return;
			}
			conn1 = hyPipe2.GetAppropriateConnector(intersectionPoint);
			conn2 = hyPipe1.GetAppropriateConnector(intersectionPoint);
		}

		public FamilyInstance NewCrossFitting(IList<Pipe> pipes, IList<XYZ> xyzs)
		{
			FamilyInstance familyInstance = null;
			SubTransaction subTransaction = null;
			FamilyInstance result;
			try
			{
				if (pipes.Count < 4)
				{
					throw new System.ArgumentNullException("Pipe", "is null");
				}
				Document document = pipes[0].Document;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0], xyzs[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1], xyzs[1]);
				YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipes[2], xyzs[2]);
				YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipes[3], xyzs[3]);
				YJKPiping<Pipe> hypiping5 = null;
				YJKPiping<Pipe> hypiping6 = null;
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				XYZ xyz = hypiping.IntersectionPoint(hypiping3.Piping, 0.001);
				if (xyz != null)
				{
					if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
					{
						new List<Pipe>();
						hypiping5 = new YJKPiping<Pipe>(((IList<Pipe>)hypiping.Separate(xyz))[1], xyzs[1]);
					}
					else if (hypiping.IsPipingOnLine(hypiping2.Piping))
					{
						hypiping5 = new YJKPiping<Pipe>(hypiping2.Piping, xyzs[1]);
					}
					if (hypiping3.Piping.UniqueId == hypiping4.Piping.UniqueId)
					{
						new List<Pipe>();
						hypiping6 = new YJKPiping<Pipe>(((IList<Pipe>)hypiping3.Separate(xyz))[1], xyzs[3]);
					}
					else if (hypiping3.IsPipingOnLine(hypiping4.Piping))
					{
						hypiping6 = new YJKPiping<Pipe>(hypiping4.Piping, xyzs[3]);
					}
					if (hypiping5 == null || hypiping6 == null)
					{
						throw new Exception("请检查水管位置是否正确");
					}
					Connector appropriateConnector = hypiping.GetAppropriateConnector(xyz);
					Connector appropriateConnector2 = hypiping5.GetAppropriateConnector(xyz);
					Connector appropriateConnector3 = hypiping3.GetAppropriateConnector(xyz);
					Connector appropriateConnector4 = hypiping6.GetAppropriateConnector(xyz);
					if (appropriateConnector == null || appropriateConnector.IsConnected || appropriateConnector2 == null || appropriateConnector2.IsConnected || appropriateConnector3 == null || appropriateConnector3.IsConnected || appropriateConnector4 == null || appropriateConnector4.IsConnected)
					{
						throw new Exception("水管已经连接");
					}
					familyInstance = document.Create.NewCrossFitting(appropriateConnector, appropriateConnector2, appropriateConnector3, appropriateConnector4);
				}
				subTransaction.Commit();
				result = familyInstance;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public FamilyInstance NewCrossFitting(ref IList<Pipe> pipes, IList<XYZ> xyzs)
		{
			FamilyInstance familyInstance = null;
			SubTransaction subTransaction = null;
			FamilyInstance result;
			try
			{
				if (pipes.Count < 4)
				{
					throw new System.ArgumentNullException("Pipe", "is null");
				}
				Document document = pipes[0].Document;
				List<Pipe> list = new List<Pipe>();
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0], xyzs[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1], xyzs[1]);
				YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipes[2], xyzs[2]);
				YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipes[3], xyzs[3]);
				YJKPiping<Pipe> hypiping5 = null;
				YJKPiping<Pipe> hypiping6 = null;
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				XYZ xyz = hypiping.IntersectionPoint(hypiping3.Piping, 0.001);
				if (xyz != null)
				{
					if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
					{
						new List<Pipe>();
						hypiping5 = new YJKPiping<Pipe>(((IList<Pipe>)hypiping.Separate(xyz))[1], xyzs[1]);
					}
					else if (hypiping.IsPipingOnLine(hypiping2.Piping))
					{
						hypiping5 = new YJKPiping<Pipe>(hypiping2.Piping, xyzs[1]);
					}
					if (hypiping3.Piping.UniqueId == hypiping4.Piping.UniqueId)
					{
						new List<Pipe>();
						hypiping6 = new YJKPiping<Pipe>(((IList<Pipe>)hypiping3.Separate(xyz))[1], xyzs[3]);
					}
					else if (hypiping3.IsPipingOnLine(hypiping4.Piping))
					{
						hypiping6 = new YJKPiping<Pipe>(hypiping4.Piping, xyzs[3]);
					}
					if (hypiping5 == null || hypiping6 == null)
					{
						throw new Exception("请检查水管位置是否正确");
					}
					Connector appropriateConnector = hypiping.GetAppropriateConnector(xyz);
					Connector appropriateConnector2 = hypiping5.GetAppropriateConnector(xyz);
					Connector appropriateConnector3 = hypiping3.GetAppropriateConnector(xyz);
					Connector appropriateConnector4 = hypiping6.GetAppropriateConnector(xyz);
					if (appropriateConnector == null || appropriateConnector.IsConnected || appropriateConnector2 == null || appropriateConnector2.IsConnected || appropriateConnector3 == null || appropriateConnector3.IsConnected || appropriateConnector4 == null || appropriateConnector4.IsConnected)
					{
						throw new Exception("水管已经连接");
					}
					familyInstance = document.Create.NewCrossFitting(appropriateConnector, appropriateConnector2, appropriateConnector3, appropriateConnector4);
					list.Add(hypiping.Piping);
					list.Add(hypiping5.Piping);
					list.Add(hypiping3.Piping);
					list.Add(hypiping6.Piping);
					pipes = list;
				}
				subTransaction.Commit();
				result = familyInstance;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public FamilyInstance NewCrossFitting(IList<Pipe> pipes, IList<XYZ> xyzs, ref string connMSG)
		{
			FamilyInstance familyInstance = null;
			SubTransaction subTransaction = null;
			FamilyInstance result;
			try
			{
				if (pipes.Count != 4)
				{
					connMSG = "四通连接，必须是四根水管！";
					result = null;
				}
				else
				{
					Document document = pipes[0].Document;
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0], xyzs[0]);
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1], xyzs[1]);
					YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipes[2], xyzs[2]);
					YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipes[3], xyzs[3]);
					YJKPiping<Pipe> hypiping5 = null;
					YJKPiping<Pipe> hypiping6 = null;
					subTransaction = new SubTransaction(document);
					subTransaction.Start();
					XYZ xyz = hypiping.IntersectionPoint(hypiping3.Piping, 0.001);
					if (xyz == null)
					{
						connMSG = "选择的四条水管，没有共同的交点，无法进行连接！";
						result = familyInstance;
					}
					else
					{
						if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
						{
							new List<Pipe>();
							hypiping5 = new YJKPiping<Pipe>(((IList<Pipe>)hypiping.Separate(xyz))[1], xyzs[1]);
						}
						else if (hypiping.IsPipingOnLine(hypiping2.Piping))
						{
							hypiping5 = new YJKPiping<Pipe>(hypiping2.Piping, xyzs[1]);
						}
						if (hypiping3.Piping.UniqueId == hypiping4.Piping.UniqueId)
						{
							new List<Pipe>();
							hypiping6 = new YJKPiping<Pipe>(((IList<Pipe>)hypiping3.Separate(xyz))[1], xyzs[3]);
						}
						else if (hypiping3.IsPipingOnLine(hypiping4.Piping))
						{
							hypiping6 = new YJKPiping<Pipe>(hypiping4.Piping, xyzs[3]);
						}
						if (!hypiping.IsPipingOnLine(hypiping5.Piping))
						{
							connMSG = "选择的第二条水管必须与第一条水管在同一直线上！";
							result = familyInstance;
						}
						else if (!hypiping3.IsPipingOnLine(hypiping6.Piping))
						{
							connMSG = "选择的第四条水管必须与第三条水管在同一直线上！";
							result = familyInstance;
						}
						else if (hypiping5 != null && hypiping6 != null)
						{
							Connector appropriateConnector = hypiping.GetAppropriateConnector(xyz);
							Connector appropriateConnector2 = hypiping5.GetAppropriateConnector(xyz);
							Connector appropriateConnector3 = hypiping3.GetAppropriateConnector(xyz);
							Connector appropriateConnector4 = hypiping6.GetAppropriateConnector(xyz);
							if (appropriateConnector != null && !appropriateConnector.IsConnected && appropriateConnector2 != null && !appropriateConnector2.IsConnected && appropriateConnector3 != null && !appropriateConnector3.IsConnected && appropriateConnector4 != null && !appropriateConnector4.IsConnected)
							{
								XYZ basisZ = appropriateConnector.CoordinateSystem.BasisZ;
								XYZ basisZ2 = appropriateConnector3.CoordinateSystem.BasisZ;
								XYZ direction = basisZ.CrossProduct(basisZ2);
								if (Math.Abs(YJKRevitTools.GetTwoVectorAngle(basisZ, basisZ2, direction) - Math.PI*.5) > 1E-05)
								{
									connMSG = "第三条水管必须垂直与第一条水管！";
									result = familyInstance;
								}
								else
								{
									familyInstance = document.Create.NewCrossFitting(appropriateConnector, appropriateConnector2, appropriateConnector3, appropriateConnector4);
									subTransaction.Commit();
									result = familyInstance;
								}
							}
							else
							{
								connMSG = "水管已经连接";
								result = familyInstance;
							}
						}
						else
						{
							connMSG = "请检查水管位置是否正确";
							result = familyInstance;
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private bool PipesConnect(IList<YJKPiping<Pipe>> orderPipes, Autodesk.Revit.DB.Document doc, ref IList<Pipe> pipes, bool bFlex = false, double flexLenght = 0.0, Dictionary<YJKPiping<Pipe>, List<FamilySymbol>> accDic = null)
		{
			bool result = false;
			pipes.Clear();
			if (orderPipes.Count > 1)
			{
				try
				{
					IList<Pipe> list = new List<Pipe>();
					int i = 0;
					while (i < orderPipes.Count)
					{
						List<FamilySymbol> list2 = null;
						if (accDic != null && accDic.Count > 0 && accDic.ContainsKey(orderPipes[i]))
						{
							list2 = accDic[orderPipes[i]];
						}
						YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(orderPipes[i].Piping);
						YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(orderPipes[i + 1].Piping);
						list.Clear();
						IList<YJKPiping<Pipe>> list3 = new List<YJKPiping<Pipe>>();
						list3.Add(hypiping);
						list3.Add(hypiping2);
						List<YJKPiping<Pipe>> list4 = null;
						switch (hypiping.CheckPipingsPosition(hypiping2.Piping))
						{
						case 10:
							if (hypiping.Piping.Diameter == hypiping2.Piping.Diameter)
							{
								hypiping.Combine(hypiping2.Piping);
								pipes.Add(hypiping.Piping);
							}
							else
							{
								list.Add(hypiping.Piping);
								list.Add(hypiping2.Piping);
								this.NewTransitionFitting(list, 1);
								doc.Regenerate();
								foreach (Pipe item in list)
								{
									pipes.Add(item);
								}
							}
							if (list2 != null && list2.Count > 0 && !new GroupValueManage(doc, hypiping2.Piping, hypiping2.GetAppropriateConnector(hypiping.Piping)[0].Origin, 100.0, list2).Go())
							{
								throw new Exception("阀件插入失败");
							}
							break;
						case 11:
						{
							IList<XYZ> appAndEndPoint = hypiping.GetAppAndEndPoint(hypiping2.Piping);
							if (appAndEndPoint != null && appAndEndPoint.Count > 0)
							{
								Pipe pipe = null;
								if (hypiping.IsPointOnPipingCurve(appAndEndPoint[1]))
								{
									pipe = YJKRevitTools.NewPipe(hypiping2.Piping, appAndEndPoint[0], appAndEndPoint[1]);
									list.Add(pipe);
									list.Add(hypiping2.Piping);
									this.NewElbowFitting(list, true);
									pipes.Add(hypiping2.Piping);
									list.Clear();
									list.Add(pipe);
									list.Add(hypiping.Piping);
									list.Add(hypiping.Piping);
									this.NewTeeFitting(ref list, true);
									foreach (Pipe item2 in list)
									{
										pipes.Add(item2);
									}
									if (list2 != null && list2.Count > 0 && !new GroupValueManage(doc, pipe, appAndEndPoint[0], 50.0, list2).Go())
									{
										throw new Exception("阀件插入失败");
									}
								}
								else
								{
									pipe = YJKRevitTools.NewPipe(hypiping.Piping, appAndEndPoint[0], appAndEndPoint[1]);
									list.Add(pipe);
									list.Add(hypiping.Piping);
									this.NewElbowFitting(list, true);
									pipes.Add(hypiping.Piping);
									list.Clear();
									list.Add(pipe);
									list.Add(hypiping2.Piping);
									list.Add(hypiping2.Piping);
									this.NewTeeFitting(ref list, true);
									foreach (Pipe item3 in list)
									{
										pipes.Add(item3);
									}
									if (list2 != null && list2.Count > 0 && !new GroupValueManage(doc, pipe, appAndEndPoint[1], 50.0, list2).Go())
									{
										throw new Exception("阀件插入失败");
									}
								}
							}
							else
							{
								IList<Connector> appropriateConnector = hypiping.GetAppropriateConnector(hypiping2.Piping);
								if (appropriateConnector != null && appropriateConnector.Count > 1)
								{
									XYZ point = (appropriateConnector[0].Origin + appropriateConnector[1].Origin) / 2.0;
									XYZ project = hypiping.GetProject(point, true);
									XYZ project2 = hypiping2.GetProject(point, true);
									YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(YJKRevitTools.NewPipe(hypiping.Piping, project, project2));
									list.Add(hypiping.Piping);
									list.Add(hypiping3.Piping);
									this.NewElbowFitting(list, true);
									doc.Regenerate();
									foreach (Pipe item4 in list)
									{
										pipes.Add(item4);
									}
									list.Clear();
									list.Add(hypiping2.Piping);
									list.Add(hypiping3.Piping);
									this.NewElbowFitting(list, true);
									doc.Regenerate();
									pipes.Add(hypiping2.Piping);
									if (list2 != null && list2.Count > 0 && !new GroupValueManage(doc, hypiping3.Piping, project2, 50.0, list2).Go())
									{
										throw new Exception("阀件插入失败");
									}
								}
							}
							break;
						}
						case 12:
						{
							list.Add(hypiping.Piping);
							list.Add(hypiping2.Piping);
							XYZ pointStart = hypiping.IntersectionPoint(hypiping2.Piping);
							if (this.CheckPipesAngle(list))
							{
								this.NewElbowFitting(list, true);
							}
							else
							{
								if (!this.CheckTwoPipesAngle(list))
								{
									throw new Exception("两连接水管角度不能小于60°");
								}
								YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
								List<XYZ> twoPipesEndPoint = this.GetTwoPipesEndPoint(list);
								hypipeConnectorHelper.connect45Elbow(list[0], twoPipesEndPoint[0], list[1], twoPipesEndPoint[1], doc);
							}
							foreach (Pipe item5 in list)
							{
								pipes.Add(item5);
							}
							if (list2 != null && list2.Count > 0 && !new GroupValueManage(doc, hypiping.Piping, pointStart, 50.0, list2).Go())
							{
								throw new Exception("阀件插入失败");
							}
							break;
						}
						case 13:
						{
							list.Add(hypiping.Piping);
							list.Add(hypiping2.Piping);
							Pipe pointOnPipe = this.GetPointOnPipe(list);
							XYZ pointStart2 = hypiping.IntersectionPoint(hypiping2.Piping);
							if (pointOnPipe != null)
							{
								list.Add(pointOnPipe);
							}
							this.NewTeeFitting(ref list, true);
							foreach (Pipe item6 in list)
							{
								pipes.Add(item6);
							}
							if (list2 != null && list2.Count > 0 && !new GroupValueManage(doc, pointOnPipe, pointStart2, 50.0, list2).Go())
							{
								throw new Exception("阀件插入失败");
							}
							break;
						}
						case 14:
							list.Add(hypiping.Piping);
							list.Add(hypiping.Piping);
							list.Add(hypiping2.Piping);
							list.Add(hypiping2.Piping);
							this.NewCrossFitting(ref list, true);
							foreach (Pipe item7 in list)
							{
								pipes.Add(item7);
							}
							if (list2 != null && list2.Count > 0 && !new GroupValueManage(doc, list[0], hypiping.IntersectionPoint(hypiping2.Piping), 50.0, list2).Go())
							{
								throw new Exception("阀件插入失败");
							}
							break;
						case 20:
						{
							IList<XYZ> endAndInsPoint = this.GetEndAndInsPoint(list3);
							if (endAndInsPoint != null && endAndInsPoint.Count > 0)
							{
								Pipe item8;
								if (hypiping.IsPointOnPipingCurve(endAndInsPoint[1]))
								{
									item8 = YJKRevitTools.NewPipe(hypiping2.Piping, endAndInsPoint[0], endAndInsPoint[1]);
									list.Add(item8);
									list.Add(hypiping2.Piping);
									this.NewElbowFitting(list, true);
									pipes.Add(hypiping2.Piping);
									list.Clear();
									list.Add(item8);
									list.Add(hypiping.Piping);
									list.Add(hypiping.Piping);
									this.NewTeeFitting(ref list, true);
									IEnumerator<Pipe> enumerator = list.GetEnumerator();
									{
										while (enumerator.MoveNext())
										{
											Pipe item9 = enumerator.Current;
											pipes.Add(item9);
										}
										break;
									}
								}
								item8 = YJKRevitTools.NewPipe(hypiping.Piping, endAndInsPoint[0], endAndInsPoint[1]);
								list.Add(item8);
								list.Add(hypiping.Piping);
								this.NewElbowFitting(list, true);
								pipes.Add(hypiping.Piping);
								list.Clear();
								list.Add(item8);
								list.Add(hypiping2.Piping);
								list.Add(hypiping2.Piping);
								this.NewTeeFitting(ref list, true);
								IEnumerator<Pipe> enumerator2 = list.GetEnumerator();
								{
									while (enumerator2.MoveNext())
									{
										Pipe item10 = enumerator2.Current;
										pipes.Add(item10);
									}
									break;
								}
							}
							IList<Connector> appropriateConnector2 = hypiping.GetAppropriateConnector(hypiping2.Piping);
							if (appropriateConnector2 != null && appropriateConnector2.Count > 1)
							{
								XYZ point2 = (appropriateConnector2[0].Origin + appropriateConnector2[1].Origin) / 2.0;
								XYZ project3 = hypiping.GetProject(point2, true);
								XYZ project4 = hypiping2.GetProject(point2, true);
								YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(YJKRevitTools.NewPipe(hypiping.Piping, project3, project4));
								list.Add(hypiping.Piping);
								list.Add(hypiping4.Piping);
								this.NewElbowFitting(list, true);
								doc.Regenerate();
								pipes.Add(hypiping.Piping);
								list.Clear();
								list.Add(hypiping2.Piping);
								list.Add(hypiping4.Piping);
								this.NewElbowFitting(list, true);
								doc.Regenerate();
								IEnumerator<Pipe> enumerator = list.GetEnumerator();
								{
									while (enumerator.MoveNext())
									{
										Pipe item11 = enumerator.Current;
										pipes.Add(item11);
									}
									break;
								}
								goto IL_A93;
							}
							break;
						}
						case 21:
							goto IL_A93;
						case 22:
							list4 = this.GetNewPipe(list3, doc);
							list.Add(hypiping.Piping);
							list.Add(list4[0].Piping);
							this.NewElbowFitting(list, true);
							pipes.Add(hypiping.Piping);
							list.Clear();
							list.Add(hypiping2.Piping);
							list.Add(list4[1].Piping);
							this.NewElbowFitting(list, true);
							foreach (Pipe item12 in list)
							{
								pipes.Add(item12);
							}
							if (list2 != null && list2.Count > 0 && !new GroupValueManage(doc, hypiping2.Piping, hypiping2.GetDistanceConnector(hypiping2.GetDefferentPlaneInstersection(list4[1].Piping)).Origin, 100.0, list2).Go())
							{
								throw new Exception("阀件插入失败");
							}
							break;
						case 23:
							list4 = this.GetNewPipe(list3, doc);
							if (hypiping.IsPointOnPipingCurve(list4[0].StartConnector.Origin) || hypiping.IsPointOnPipingCurve(list4[0].EndConnector.Origin))
							{
								list.Add(hypiping.Piping);
								list.Add(hypiping.Piping);
								list.Add(list4[0].Piping);
								this.NewTeeFitting(ref list, true);
								foreach (Pipe item13 in list)
								{
									pipes.Add(item13);
								}
								list.Clear();
								list.Add(hypiping2.Piping);
								list.Add(list4[1].Piping);
								this.NewElbowFitting(list, true);
								doc.Regenerate();
								if (list2 != null && list2.Count > 0)
								{
									if (hypiping2.StartConnector.Direction.AngleTo(hypiping.StartConnector.Direction) == Math.PI*.5)
									{
										if (!new GroupValueManage(doc, hypiping2.Piping, hypiping2.GetDistanceConnector(hypiping2.GetDefferentPlaneInstersection(list4[1].Piping)).Origin, 100.0, list2, false).Go() && !new GroupValueManage(doc, list4[1].Piping, hypiping2.GetDefferentPlaneInstersection(list4[1].Piping), 100.0, list2).Go())
										{
											throw new Exception("阀件插入失败");
										}
									}
									else if (!new GroupValueManage(doc, hypiping2.Piping, hypiping2.GetDistanceConnector(hypiping2.GetDefferentPlaneInstersection(list4[1].Piping)).Origin, 100.0, list2).Go())
									{
										throw new Exception("阀件插入失败");
									}
								}
							}
							else
							{
								list.Add(hypiping.Piping);
								list.Add(list4[0].Piping);
								this.NewElbowFitting(list, true);
								list.Clear();
								list.Add(hypiping2.Piping);
								list.Add(hypiping2.Piping);
								list.Add(list4[1].Piping);
								this.NewTeeFitting(ref list, true);
								foreach (Pipe item14 in list)
								{
									pipes.Add(item14);
								}
								if (list2 != null && list2.Count > 0 && !new GroupValueManage(doc, hypiping2.Piping, hypiping2.GetDistanceConnector(hypiping2.GetDefferentPlaneInstersection(list4[1].Piping)).Origin, 100.0, list2).Go())
								{
									throw new Exception("阀件插入失败");
								}
							}
							break;
						case 24:
							list4 = this.GetNewPipe(list3, doc);
							list.Add(hypiping.Piping);
							list.Add(hypiping.Piping);
							list.Add(list4[0].Piping);
							this.NewTeeFitting(ref list, true);
							foreach (Pipe item15 in list)
							{
								pipes.Add(item15);
							}
							list.Clear();
							list.Add(hypiping2.Piping);
							list.Add(hypiping2.Piping);
							list.Add(list4[1].Piping);
							this.NewTeeFitting(ref list, true);
							foreach (Pipe item16 in list)
							{
								pipes.Add(item16);
							}
							if (list2 != null && list2.Count > 0 && !new GroupValueManage(doc, list4[1].Piping, hypiping2.GetDefferentPlaneInstersection(list4[1].Piping), 50.0, list2).Go())
							{
								throw new Exception("阀件插入失败");
							}
							break;
						}
						IL_150B:
						result = true;
						i += 2;
						continue;
						IL_A93:
						IList<XYZ> appAndEndPoint2 = hypiping.GetAppAndEndPoint(hypiping2.Piping);
						if (appAndEndPoint2 != null && appAndEndPoint2.Count > 0)
						{
							XYZ endXYZ = new XYZ(appAndEndPoint2[1].X, appAndEndPoint2[1].Y, appAndEndPoint2[0].Z);
							if (hypiping.IsPointOnPipingCurve(appAndEndPoint2[1]))
							{
								Pipe pipe2 = YJKRevitTools.NewPipe(hypiping2.Piping, appAndEndPoint2[0], endXYZ);
								Pipe item17 = YJKRevitTools.NewPipe(hypiping2.Piping, appAndEndPoint2[1], endXYZ);
								list.Add(hypiping2.Piping);
								list.Add(pipe2);
								this.NewElbowFitting(list, true);
								doc.Regenerate();
								pipes.Add(hypiping2.Piping);
								list.Clear();
								list.Add(pipe2);
								list.Add(item17);
								this.NewElbowFitting(list, true);
								doc.Regenerate();
								pipes.Add(item17);
								list.Clear();
								list.Add(hypiping.Piping);
								list.Add(hypiping.Piping);
								list.Add(item17);
								this.NewTeeFitting(ref list, true);
								foreach (Pipe item18 in list)
								{
									pipes.Add(item18);
								}
								if (list2 != null && list2.Count > 0 && !new GroupValueManage(doc, pipe2, appAndEndPoint2[0], 100.0, list2).Go())
								{
									throw new Exception("阀件插入失败");
								}
								goto IL_150B;
							}
							else
							{
								Pipe pipe3 = YJKRevitTools.NewPipe(hypiping.Piping, appAndEndPoint2[0], endXYZ);
								Pipe item19 = YJKRevitTools.NewPipe(hypiping.Piping, appAndEndPoint2[1], endXYZ);
								list.Add(hypiping.Piping);
								list.Add(pipe3);
								this.NewElbowFitting(list, true);
								doc.Regenerate();
								pipes.Add(hypiping.Piping);
								list.Clear();
								list.Add(pipe3);
								list.Add(item19);
								this.NewElbowFitting(list, true);
								doc.Regenerate();
								pipes.Add(item19);
								list.Clear();
								list.Add(hypiping2.Piping);
								list.Add(hypiping2.Piping);
								list.Add(item19);
								this.NewTeeFitting(ref list, true);
								foreach (Pipe item20 in list)
								{
									pipes.Add(item20);
								}
								if (list2 != null && list2.Count > 0 && !new GroupValueManage(doc, pipe3, appAndEndPoint2[0], 50.0, list2).Go())
								{
									throw new Exception("阀件插入失败");
								}
								goto IL_150B;
							}
						}
						else
						{
							IList<XYZ> point3 = this.GetPoint21(list3);
							if (point3 == null || point3.Count <= 0)
							{
								goto IL_150B;
							}
							Pipe pipe4 = YJKRevitTools.NewPipe(hypiping.Piping, point3[0], point3[2]);
							Pipe pipe5 = YJKRevitTools.NewPipe(hypiping.Piping, point3[1], point3[2]);
							if (hypiping.IsPointOnPipingLine(point3[0]))
							{
								list.Add(hypiping.Piping);
								list.Add(pipe4);
								this.NewElbowFitting(list, true);
								doc.Regenerate();
								pipes.Add(hypiping.Piping);
								list.Clear();
								list.Add(pipe4);
								list.Add(pipe5);
								this.NewElbowFitting(list, true);
								doc.Regenerate();
								pipes.Add(pipe4);
								list.Clear();
								list.Add(pipe5);
								list.Add(hypiping2.Piping);
								this.NewElbowFitting(list, true);
								doc.Regenerate();
								foreach (Pipe item21 in list)
								{
									pipes.Add(item21);
								}
								if (list2 != null && list2.Count > 0 && !new GroupValueManage(doc, pipe5, point3[1], 50.0, list2, true).Go())
								{
									throw new Exception("阀件插入失败");
								}
								goto IL_150B;
							}
							else
							{
								list.Add(hypiping.Piping);
								list.Add(pipe5);
								this.NewElbowFitting(list, true);
								doc.Regenerate();
								pipes.Add(hypiping.Piping);
								list.Clear();
								list.Add(pipe4);
								list.Add(pipe5);
								this.NewElbowFitting(list, true);
								doc.Regenerate();
								pipes.Add(pipe5);
								list.Clear();
								list.Add(pipe4);
								list.Add(hypiping2.Piping);
								this.NewElbowFitting(list, true);
								doc.Regenerate();
								foreach (Pipe item22 in list)
								{
									pipes.Add(item22);
								}
								if (list2 != null && list2.Count > 0 && !new GroupValueManage(doc, pipe4, point3[0], 50.0, list2).Go())
								{
									throw new Exception("阀件插入失败");
								}
								goto IL_150B;
							}
						}
					}
					return result;
				}
				catch (Exception ex)
				{
					throw ex;
				}
			}
			throw new Exception("没有找到适合的管道匹配类型，或者管道已连接。");
		}

		private bool CheckPipesAngle(IList<Pipe> pipes)
		{
			bool result = true;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
			XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			XYZ xyz2 = (origin.DistanceTo(xyz) > origin2.DistanceTo(xyz)) ? origin2.Subtract(origin).Normalize() : origin.Subtract(origin2).Normalize();
			XYZ origin3 = hypiping2.StartConnector.Origin;
			XYZ origin4 = hypiping2.EndConnector.Origin;
			XYZ xyz3 = (origin3.DistanceTo(xyz) > origin4.DistanceTo(xyz)) ? origin4.Subtract(origin3).Normalize() : origin3.Subtract(origin4).Normalize();
			if (YJKRevitTools.GetTwoVectorAngle(xyz2, xyz3, xyz2.CrossProduct(xyz3)) / Math.PI * 180.0 < 89.0)
			{
				result = false;
			}
			return result;
		}

		private bool CheckTwoPipesAngle(IList<Pipe> pipes)
		{
			bool result = true;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
			XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			XYZ xyz2 = (origin.DistanceTo(xyz) > origin2.DistanceTo(xyz)) ? origin2.Subtract(origin).Normalize() : origin.Subtract(origin2).Normalize();
			XYZ origin3 = hypiping2.StartConnector.Origin;
			XYZ origin4 = hypiping2.EndConnector.Origin;
			XYZ xyz3 = (origin3.DistanceTo(xyz) > origin4.DistanceTo(xyz)) ? origin4.Subtract(origin3).Normalize() : origin3.Subtract(origin4).Normalize();
			if (YJKRevitTools.GetTwoVectorAngle(xyz2, xyz3, xyz2.CrossProduct(xyz3)) / Math.PI * 180.0 < 60.0)
			{
				result = false;
			}
			return result;
		}

		private List<XYZ> GetTwoPipesEndPoint(IList<Pipe> pipes)
		{
			List<XYZ> list = new List<XYZ>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
			XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			XYZ item = (origin.DistanceTo(xyz) > origin2.DistanceTo(xyz)) ? origin2 : origin;
			XYZ origin3 = hypiping2.StartConnector.Origin;
			XYZ origin4 = hypiping2.EndConnector.Origin;
			XYZ item2 = (origin3.DistanceTo(xyz) > origin4.DistanceTo(xyz)) ? origin4 : origin3;
			list.Add(item);
			list.Add(item2);
			return list;
		}

		public bool NewBeatchFitting(ref IList<Pipe> pipes)
		{
			bool result;
			try
			{
				if (pipes == null || pipes.Count < 2)
				{
					throw new System.ArgumentNullException("Pipe", "is null");
				}
				Document document = pipes[0].Document;
				IList<YJKPiping<Pipe>> orderPipes = new List<YJKPiping<Pipe>>();
				GroupPipe<Pipe> groupPipe = new GroupPipe<Pipe>(pipes);
				groupPipe.Execute();
				orderPipes = groupPipe.sortedPipes;
				result = this.PipesConnect(orderPipes, document, ref pipes, false, 0.0, null);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public bool NewSomeConnect(IList<Pipe> pipes)
		{
			bool flag = true;
			bool result;
			try
			{
				if (pipes == null || pipes.Count < 2)
				{
					throw new Exception("最少选择2根水管");
				}
				Document document = pipes[0].Document;
				IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
				list = this.SomePipesOrderBy(pipes);
				if (list != null && list.Count > 1)
				{
					flag = this.PipesConnect(list, document, ref pipes, false, 0.0, null);
				}
				result = flag;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public List<Pipe> NewSomeConnect(IList<Pipe> pipes, ref string errorMSG)
		{
			List<Pipe> result;
			try
			{
				Document document = pipes[0].Document;
				IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
				list = this.SomePipesOrderBy(pipes);
				if (list != null && list.Count > 1)
				{
					this.PipesConnect(list, document, ref pipes, false, 0.0, null);
				}
				result = pipes.ToList<Pipe>();
			}
			catch (Exception)
			{
				errorMSG = "管道位置不合适";
				result = null;
			}
			return result;
		}

		private IList<XYZ> GetPoint21(IList<YJKPiping<Pipe>> hyPipes)
		{
			IList<XYZ> list = new List<XYZ>();
			IList<Connector> appropriateConnector = hyPipes[0].GetAppropriateConnector(hyPipes[1].Piping);
			if (appropriateConnector != null && appropriateConnector.Count > 1)
			{
				if (Math.Abs(hyPipes[0].StartConnector.Origin.X - hyPipes[0].EndConnector.Origin.X) < 0.0001 && Math.Abs(hyPipes[0].StartConnector.Origin.Y - hyPipes[0].EndConnector.Origin.Y) < 0.0001)
				{
					XYZ xyz = new XYZ(appropriateConnector[0].Origin.X, appropriateConnector[1].Origin.Y, (appropriateConnector[0].Origin.Z + appropriateConnector[1].Origin.Z) / 2.0);
					XYZ project = hyPipes[0].GetProject(xyz, true);
					XYZ project2 = hyPipes[1].GetProject(xyz, true);
					list.Add(project);
					list.Add(project2);
					list.Add(xyz);
				}
				else
				{
					XYZ xyz;
					if (!appropriateConnector[0].IsConnected && !appropriateConnector[1].IsConnected)
					{
						xyz = (appropriateConnector[0].Origin + appropriateConnector[1].Origin) / 2.0;
					}
					else if (appropriateConnector[0].IsConnected)
					{
						xyz = hyPipes[0].GetDistanceConnector(appropriateConnector[0].Origin).Origin;
					}
					else
					{
						xyz = hyPipes[1].GetDistanceConnector(appropriateConnector[1].Origin).Origin;
					}
					XYZ project3 = hyPipes[0].GetProject(xyz, true);
					XYZ project4 = hyPipes[1].GetProject(xyz, true);
					XYZ item = new XYZ(project4.X, project4.Y, project3.Z);
					list.Add(project3);
					list.Add(project4);
					list.Add(item);
				}
			}
			return list;
		}

		private IList<YJKPiping<Pipe>> SomePipesOrderBy(IList<Pipe> pipes)
		{
			IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
			switch (pipes.Count)
			{
			case 1:
				throw new Exception("请选择多根水管");
			case 2:
			{
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
				if (hypiping.IsPipingOnLine(hypiping2.Piping))
				{
					IList<Pipe> list2 = new List<Pipe>();
					if (hypiping.Piping.Diameter == hypiping2.Piping.Diameter)
					{
						hypiping.Combine(hypiping2.Piping);
					}
					else
					{
						list2.Add(hypiping.Piping);
						list2.Add(hypiping2.Piping);
						this.NewTransitionFitting(list2, 1);
					}
				}
				else
				{
					list.Add(hypiping);
					list.Add(hypiping2);
				}
				break;
			}
			case 3:
			{
				YJKPiping<Pipe> hyPipe = new YJKPiping<Pipe>(pipes[0]);
				YJKPiping<Pipe> hyPipe2 = new YJKPiping<Pipe>(pipes[1]);
				YJKPiping<Pipe> hyPipe3 = new YJKPiping<Pipe>(pipes[2]);
				list = this.OrderThreePipe(hyPipe, hyPipe2, hyPipe3);
				break;
			}
			case 4:
			{
				IList<Pipe> list3 = new List<Pipe>();
				int num = 0;
				YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipes[0]);
				YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipes[1]);
				YJKPiping<Pipe> hypiping5 = new YJKPiping<Pipe>(pipes[2]);
				YJKPiping<Pipe> hypiping6 = new YJKPiping<Pipe>(pipes[3]);
				if (hypiping3.IsPipingOnLine(hypiping4.Piping))
				{
					if (hypiping3.Piping.Diameter == hypiping4.Piping.Diameter && hypiping3.GetSystemElementId() == hypiping4.GetSystemElementId())
					{
						hypiping3.Combine(hypiping4.Piping);
						num++;
					}
					if (hypiping5.Piping.Diameter == hypiping6.Piping.Diameter && hypiping5.GetSystemElementId() == hypiping6.GetSystemElementId())
					{
						hypiping5.Combine(hypiping6.Piping);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(ref pipes, true);
					}
					else if (1 == num)
					{
						if (!hypiping3.IsIntersection(hypiping5.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping5.Piping);
						list3.Add(hypiping6.Piping);
						this.NewCrossFitting(ref list3, true);
					}
					else if (2 == num)
					{
						if (!hypiping3.IsIntersection(hypiping5.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping5.Piping);
						list3.Add(hypiping5.Piping);
						this.NewCrossFitting(ref list3, true);
					}
					else if (3 == num)
					{
						list.Add(hypiping3);
						list.Add(hypiping5);
					}
				}
				else if (hypiping3.IsPipingOnLine(hypiping5.Piping))
				{
					if (hypiping3.Piping.Diameter == hypiping5.Piping.Diameter && hypiping3.GetSystemElementId() == hypiping5.GetSystemElementId())
					{
						hypiping3.Combine(hypiping5.Piping);
						num++;
					}
					if (hypiping4.Piping.Diameter == hypiping6.Piping.Diameter && hypiping4.GetSystemElementId() == hypiping6.GetSystemElementId())
					{
						hypiping4.Combine(hypiping6.Piping);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(ref pipes, true);
					}
					else if (1 == num)
					{
						if (!hypiping3.IsIntersection(hypiping4.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping6.Piping);
						this.NewCrossFitting(ref list3, true);
					}
					else if (2 == num)
					{
						if (!hypiping3.IsIntersection(hypiping4.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping5.Piping);
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping4.Piping);
						this.NewCrossFitting(ref list3, true);
					}
					else if (3 == num)
					{
						list.Add(hypiping3);
						list.Add(hypiping4);
					}
				}
				else if (hypiping3.IsPipingOnLine(hypiping6.Piping))
				{
					if (hypiping3.Piping.Diameter == hypiping6.Piping.Diameter && hypiping3.GetSystemElementId() == hypiping6.GetSystemElementId())
					{
						hypiping3.Combine(hypiping6.Piping);
						num++;
					}
					if (hypiping4.Piping.Diameter == hypiping5.Piping.Diameter && hypiping4.GetSystemElementId() == hypiping5.GetSystemElementId())
					{
						hypiping4.Combine(hypiping5.Piping);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(ref pipes, true);
					}
					else if (1 == num)
					{
						if (!hypiping3.IsIntersection(hypiping4.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping5.Piping);
						this.NewCrossFitting(ref list3, true);
					}
					else if (2 == num)
					{
						if (!hypiping3.IsIntersection(hypiping4.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping5.Piping);
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping4.Piping);
						this.NewCrossFitting(ref list3, true);
					}
					else if (3 == num)
					{
						list.Add(hypiping3);
						list.Add(hypiping4);
					}
				}
				else if (hypiping4.IsPipingOnLine(hypiping5.Piping))
				{
					if (hypiping4.Piping.Diameter == hypiping5.Piping.Diameter && hypiping4.GetSystemElementId() == hypiping5.GetSystemElementId())
					{
						hypiping4.Combine(hypiping5.Piping);
						num++;
					}
					if (hypiping3.Piping.Diameter == hypiping6.Piping.Diameter && hypiping3.GetSystemElementId() == hypiping6.GetSystemElementId())
					{
						hypiping3.Combine(hypiping6.Piping);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(ref pipes, true);
					}
					else if (1 == num)
					{
						if (!hypiping3.IsIntersection(hypiping4.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping6.Piping);
						this.NewCrossFitting(ref list3, true);
					}
					else if (2 == num)
					{
						if (!hypiping3.IsIntersection(hypiping4.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping5.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping3.Piping);
						this.NewCrossFitting(ref list3, true);
					}
					else if (3 == num)
					{
						list.Add(hypiping3);
						list.Add(hypiping4);
					}
				}
				else if (hypiping4.IsPipingOnLine(hypiping6.Piping))
				{
					if (hypiping4.Piping.Diameter == hypiping6.Piping.Diameter && hypiping4.GetSystemElementId() == hypiping6.GetSystemElementId())
					{
						hypiping4.Combine(hypiping6.Piping);
						num++;
					}
					if (hypiping3.Piping.Diameter == hypiping5.Piping.Diameter && hypiping3.GetSystemElementId() == hypiping5.GetSystemElementId())
					{
						hypiping3.Combine(hypiping5.Piping);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(ref pipes, true);
					}
					else if (1 == num)
					{
						if (!hypiping3.IsIntersection(hypiping4.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping5.Piping);
						this.NewCrossFitting(ref list3, true);
					}
					else if (2 == num)
					{
						if (!hypiping3.IsIntersection(hypiping4.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping6.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping3.Piping);
						this.NewCrossFitting(ref list3, true);
					}
					else if (3 == num)
					{
						list.Add(hypiping3);
						list.Add(hypiping4);
					}
				}
				else if (hypiping5.IsPipingOnLine(hypiping6.Piping))
				{
					if (hypiping5.Piping.Diameter == hypiping6.Piping.Diameter && hypiping5.GetSystemElementId() == hypiping6.GetSystemElementId())
					{
						hypiping5.Combine(hypiping6.Piping);
						num++;
					}
					if (hypiping3.Piping.Diameter == hypiping4.Piping.Diameter && hypiping3.GetSystemElementId() == hypiping4.GetSystemElementId())
					{
						hypiping3.Combine(hypiping4.Piping);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(ref pipes, true);
					}
					else if (1 == num)
					{
						if (!hypiping3.IsIntersection(hypiping5.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping5.Piping);
						list3.Add(hypiping5.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping4.Piping);
						this.NewCrossFitting(ref list3, true);
					}
					else if (2 == num)
					{
						if (!hypiping3.IsIntersection(hypiping5.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping5.Piping);
						list3.Add(hypiping6.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping3.Piping);
						this.NewCrossFitting(ref list3, true);
					}
					else if (3 == num)
					{
						list.Add(hypiping3);
						list.Add(hypiping5);
					}
				}
				else if (hypiping3.IsIntersection(hypiping4.Piping))
				{
					list.Add(hypiping3);
					list.Add(hypiping4);
				}
				else if (hypiping3.IsIntersection(hypiping5.Piping))
				{
					list.Add(hypiping3);
					list.Add(hypiping5);
				}
				else if (hypiping3.IsIntersection(hypiping6.Piping))
				{
					list.Add(hypiping3);
					list.Add(hypiping6);
				}
				else if (hypiping4.IsIntersection(hypiping5.Piping))
				{
					list.Add(hypiping4);
					list.Add(hypiping5);
				}
				else if (hypiping4.IsIntersection(hypiping6.Piping))
				{
					list.Add(hypiping4);
					list.Add(hypiping6);
				}
				else if (hypiping5.IsIntersection(hypiping6.Piping))
				{
					list.Add(hypiping5);
					list.Add(hypiping6);
				}
				else
				{
					list.Add(hypiping3);
					list.Add(hypiping4);
				}
				break;
			}
			default:
				throw new Exception("水管不能超过四根");
			}
			return list;
		}

		private IList<YJKPiping<Pipe>> OrderThreePipe(YJKPiping<Pipe> hyPipe, YJKPiping<Pipe> hyPipe1, YJKPiping<Pipe> hyPipe2)
		{
			IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
			IList<Pipe> list2 = new List<Pipe>();
			list2.Add(hyPipe.Piping);
			list2.Add(hyPipe1.Piping);
			list2.Add(hyPipe2.Piping);
			if (hyPipe.IsPipingOnLine(hyPipe1.Piping))
			{
				if (hyPipe.Piping.Diameter == hyPipe1.Piping.Diameter && hyPipe.GetSystemElementId() == hyPipe1.GetSystemElementId())
				{
					hyPipe.Combine(hyPipe1.Piping);
					list.Add(hyPipe);
					list.Add(hyPipe2);
				}
				else
				{
					this.NewTeeFitting(ref list2, true);
					list = null;
				}
			}
			else if (hyPipe.IsPipingOnLine(hyPipe2.Piping))
			{
				if (hyPipe.Piping.Diameter == hyPipe2.Piping.Diameter && hyPipe.GetSystemElementId() == hyPipe2.GetSystemElementId())
				{
					hyPipe.Combine(hyPipe2.Piping);
					list.Add(hyPipe);
					list.Add(hyPipe1);
				}
				else
				{
					this.NewTeeFitting(ref list2, true);
					list = null;
				}
			}
			else if (hyPipe1.IsPipingOnLine(hyPipe2.Piping))
			{
				if (hyPipe1.Piping.Diameter == hyPipe2.Piping.Diameter && hyPipe1.GetSystemElementId() == hyPipe2.GetSystemElementId())
				{
					hyPipe1.Combine(hyPipe2.Piping);
					list.Add(hyPipe);
					list.Add(hyPipe1);
				}
				else
				{
					this.NewTeeFitting(ref list2, true);
					list = null;
				}
			}
			else if (hyPipe.IsIntersection(hyPipe1.Piping))
			{
				list.Add(hyPipe);
				list.Add(hyPipe1);
			}
			else if (hyPipe.IsIntersection(hyPipe2.Piping))
			{
				list.Add(hyPipe);
				list.Add(hyPipe2);
			}
			else if (hyPipe1.IsIntersection(hyPipe2.Piping))
			{
				list.Add(hyPipe2);
				list.Add(hyPipe1);
			}
			else
			{
				list.Add(hyPipe);
				list.Add(hyPipe1);
			}
			return list;
		}

		private IList<XYZ> GetEndAndInsPoint(IList<YJKPiping<Pipe>> hyPipes)
		{
			IList<XYZ> list = new List<XYZ>();
			YJKPiping<Pipe> hypiping = hyPipes[0];
			YJKPiping<Pipe> hypiping2 = hyPipes[1];
			bool flag = false;
			if (hypiping.GetDiameter() > hypiping2.GetDiameter())
			{
				if (!hypiping2.StartConnector.Connector.IsConnected)
				{
					XYZ xyz = new XYZ(hypiping2.StartConnector.Origin.X, hypiping2.StartConnector.Origin.Y, hypiping.StartConnector.Origin.Z);
					XYZ project = hypiping.GetProject(hypiping2.StartConnector.Origin);
					if (project != null && xyz.IsAlmostEqualTo(project))
					{
						list.Add(hypiping2.StartConnector.Origin);
						list.Add(project);
						flag = true;
					}
				}
				if (!hypiping2.EndConnector.Connector.IsConnected && !flag)
				{
					XYZ xyz2 = new XYZ(hypiping2.EndConnector.Origin.X, hypiping2.EndConnector.Origin.Y, hypiping.EndConnector.Origin.Z);
					XYZ project2 = hypiping.GetProject(hypiping2.EndConnector.Origin);
					if (project2 != null && xyz2.IsAlmostEqualTo(project2))
					{
						list.Add(hypiping2.EndConnector.Origin);
						list.Add(project2);
						flag = true;
					}
				}
				if (!hypiping.StartConnector.Connector.IsConnected && !flag)
				{
					XYZ xyz3 = new XYZ(hypiping.StartConnector.Origin.X, hypiping.StartConnector.Origin.Y, hypiping2.StartConnector.Origin.Z);
					XYZ project3 = hypiping2.GetProject(hypiping.StartConnector.Origin);
					if (project3 != null && xyz3.IsAlmostEqualTo(project3))
					{
						list.Add(hypiping.StartConnector.Origin);
						list.Add(project3);
						flag = true;
					}
				}
				if (!hypiping.EndConnector.Connector.IsConnected && !flag)
				{
					XYZ xyz4 = new XYZ(hypiping.EndConnector.Origin.X, hypiping.EndConnector.Origin.Y, hypiping2.EndConnector.Origin.Z);
					XYZ project4 = hypiping2.GetProject(hypiping.EndConnector.Origin);
					if (project4 != null && xyz4.IsAlmostEqualTo(project4))
					{
						list.Add(hypiping.EndConnector.Origin);
						list.Add(project4);
					}
				}
			}
			else
			{
				if (!hypiping.StartConnector.Connector.IsConnected)
				{
					XYZ xyz5 = new XYZ(hypiping.StartConnector.Origin.X, hypiping.StartConnector.Origin.Y, hypiping2.StartConnector.Origin.Z);
					XYZ project5 = hypiping2.GetProject(hypiping.StartConnector.Origin);
					if (project5 != null && xyz5.IsAlmostEqualTo(project5))
					{
						list.Add(hypiping.StartConnector.Origin);
						list.Add(project5);
						flag = true;
					}
				}
				if (!hypiping.EndConnector.Connector.IsConnected && !flag)
				{
					XYZ xyz6 = new XYZ(hypiping.EndConnector.Origin.X, hypiping.EndConnector.Origin.Y, hypiping2.EndConnector.Origin.Z);
					XYZ project6 = hypiping2.GetProject(hypiping.EndConnector.Origin);
					if (project6 != null && xyz6.IsAlmostEqualTo(project6))
					{
						list.Add(hypiping.EndConnector.Origin);
						list.Add(project6);
						flag = true;
					}
				}
				if (!hypiping2.StartConnector.Connector.IsConnected && !flag)
				{
					XYZ xyz7 = new XYZ(hypiping2.StartConnector.Origin.X, hypiping2.StartConnector.Origin.Y, hypiping.StartConnector.Origin.Z);
					XYZ project7 = hypiping.GetProject(hypiping2.StartConnector.Origin);
					if (project7 != null && xyz7.IsAlmostEqualTo(project7))
					{
						list.Add(hypiping2.StartConnector.Origin);
						list.Add(project7);
						flag = true;
					}
				}
				if (!hypiping2.EndConnector.Connector.IsConnected && !flag)
				{
					XYZ xyz8 = new XYZ(hypiping2.EndConnector.Origin.X, hypiping2.EndConnector.Origin.Y, hypiping.EndConnector.Origin.Z);
					XYZ project8 = hypiping.GetProject(hypiping2.EndConnector.Origin);
					if (project8 != null && xyz8.IsAlmostEqualTo(project8))
					{
						list.Add(hypiping2.EndConnector.Origin);
						list.Add(project8);
					}
				}
			}
			return list;
		}

		private List<YJKPiping<Pipe>> GetNewPipe(IList<YJKPiping<Pipe>> hyPipes, Autodesk.Revit.DB.Document doc)
		{
			List<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
			XYZ defferentPlaneInstersection = hyPipes[0].GetDefferentPlaneInstersection(hyPipes[1].Piping);
			XYZ defferentPlaneInstersection2 = hyPipes[1].GetDefferentPlaneInstersection(hyPipes[0].Piping);
			Pipe sourcePipe = (hyPipes[0].GetDiameter() < hyPipes[1].GetDiameter()) ? hyPipes[0].Piping : hyPipes[1].Piping;
			Connector closeConnector = hyPipes[0].GetCloseConnector(defferentPlaneInstersection, false);
			Connector closeConnector2 = hyPipes[1].GetCloseConnector(defferentPlaneInstersection2, false);
			if (closeConnector.IsConnected && !hyPipes[0].IsPointOnPipingCurve(defferentPlaneInstersection))
			{
				Connector distanceConnector = hyPipes[0].GetDistanceConnector(defferentPlaneInstersection);
				XYZ xyz = (distanceConnector.Origin - closeConnector.Origin).Normalize();
				double num = distanceConnector.Origin.DistanceTo(defferentPlaneInstersection) + 0.3;
				XYZ startXYZ = defferentPlaneInstersection + xyz * num;
				XYZ endXYZ = defferentPlaneInstersection2 + xyz * num;
				Pipe pipe = YJKRevitTools.NewPipe(sourcePipe, startXYZ, endXYZ);
				Pipe pipe2 = YJKRevitTools.NewPipe(sourcePipe, defferentPlaneInstersection2, endXYZ);
				this.NewElbowFitting(new List<Pipe>
				{
					pipe,
					pipe2
				}, true);
				list.Add(new YJKPiping<Pipe>(pipe));
				list.Add(new YJKPiping<Pipe>(pipe2));
			}
			else if (closeConnector2.IsConnected && !hyPipes[1].IsPointOnPipingCurve(defferentPlaneInstersection2))
			{
				Connector distanceConnector2 = hyPipes[1].GetDistanceConnector(defferentPlaneInstersection2);
				XYZ xyz2 = (distanceConnector2.Origin - closeConnector2.Origin).Normalize();
				double num2 = distanceConnector2.Origin.DistanceTo(defferentPlaneInstersection2) + 0.3;
				XYZ xyz3 = defferentPlaneInstersection + xyz2 * num2;
				XYZ endXYZ2 = defferentPlaneInstersection2 + xyz2 * num2;
				Pipe pipe3 = YJKRevitTools.NewPipe(sourcePipe, xyz3, endXYZ2);
				Pipe pipe4 = YJKRevitTools.NewPipe(sourcePipe, defferentPlaneInstersection, xyz3);
				this.NewElbowFitting(new List<Pipe>
				{
					pipe3,
					pipe4
				}, true);
				list.Add(new YJKPiping<Pipe>(pipe4));
				list.Add(new YJKPiping<Pipe>(pipe3));
			}
			else
			{
				YJKPiping<Pipe> item = new YJKPiping<Pipe>(YJKRevitTools.NewPipe(sourcePipe, defferentPlaneInstersection, defferentPlaneInstersection2));
				list.Add(item);
				list.Add(item);
			}
			return list;
		}

		private Pipe GetPointOnPipe(IList<Pipe> hyPipes)
		{
			Pipe result = null;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(hyPipes[0]);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(hyPipes[1]);
			if (hyPipes.Count == 2)
			{
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping, 0.03);
				if (xyz != null)
				{
					if (hypiping.IsPointOnPipingCurve(xyz))
					{
						result = hypiping.Piping;
					}
					else if (hypiping2.IsPointOnPipingCurve(xyz))
					{
						result = hypiping2.Piping;
					}
				}
			}
			return result;
		}

		private IList<YJKPiping<Pipe>> PipesOrderByType(IList<Pipe> pipes)
		{
			IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
			IList<YJKPiping<Pipe>> list2 = new List<YJKPiping<Pipe>>();
			foreach (Pipe piping in pipes)
			{
				list.Add(new YJKPiping<Pipe>(piping));
			}
			list = (from c in list
			orderby c.GetSystemElementId().IntegerValue
			select c).ToList<YJKPiping<Pipe>>();
			int count = list.Count;
			for (int i = 0; i < count; i += 2)
			{
				if (i + 1 < count)
				{
					if (list[i].GetSystemElementId().IntegerValue == list[i + 1].GetSystemElementId().IntegerValue)
					{
						list2.Add(list[i]);
						list2.Add(list[i + 1]);
						for (int j = i + 1; j < count; j++)
						{
							if (j + 1 < count && list[j].GetSystemElementId().IntegerValue != list[j + 1].GetSystemElementId().IntegerValue)
							{
								i = j - 1;
								break;
							}
						}
					}
					else
					{
						i--;
					}
				}
			}
			return list2;
		}

		private Connector GetHSprinklersConnector(FamilyInstance fi)
		{
			Connector result = null;
			ConnectorSetIterator connectorSetIterator = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
			while (connectorSetIterator.MoveNext())
			{
				object obj = connectorSetIterator.Current;
				Connector connector = obj as Connector;
				if (connector != null)
				{
					result = connector;
					break;
				}
			}
			return result;
		}

		private XYZ GetLocationPoint(FamilyInstance fi)
		{
			return (fi.Location as LocationPoint).Point;
		}

		private XYZ GetConnectorDirectionPoint(Connector connector)
		{
			XYZ result = null;
			if (connector != null)
			{
				Transform coordinateSystem = connector.CoordinateSystem;
				result = connector.Origin + 0.1 * coordinateSystem.BasisZ;
			}
			return result;
		}

		private XYZ GetConnectorDirectionPoint(Connector connector, double distance)
		{
			XYZ result = null;
			if (connector != null)
			{
				Transform coordinateSystem = connector.CoordinateSystem;
				result = connector.Origin + distance * coordinateSystem.BasisZ;
			}
			return result;
		}

		private XYZ GetConnectorDirectionPoint(Connector connector, double distance, bool bFlex, double flexLength)
		{
			XYZ result = null;
			if (connector != null)
			{
				Transform coordinateSystem = connector.CoordinateSystem;
				if (bFlex)
				{
					result = connector.Origin + distance * coordinateSystem.BasisZ + UnitConvert.LengthToStd(flexLength / 1000.0) * coordinateSystem.BasisZ;
				}
				else
				{
					result = connector.Origin + distance * coordinateSystem.BasisZ;
				}
			}
			return result;
		}

		private XYZ GetFlexConnectorDirectionPoint(Connector connector, bool bFlex, double flexLength)
		{
			XYZ result = null;
			if (connector != null)
			{
				Transform coordinateSystem = connector.CoordinateSystem;
				if (bFlex)
				{
					result = connector.Origin + UnitConvert.LengthToStd(flexLength / 1000.0) * coordinateSystem.BasisZ;
				}
				else
				{
					result = connector.Origin;
				}
			}
			return result;
		}

		private XYZ GetCurveIntersection(Curve curve1, Curve curve2)
		{
			XYZ result = new XYZ();
			result = null;
			IntersectionResultArray intersectionResultArray = new IntersectionResultArray();
			SetComparisonResult setComparisonResult = curve1.Intersect(curve2, out intersectionResultArray);
			if (4 != (int)setComparisonResult && !intersectionResultArray.IsEmpty)
			{
				result = intersectionResultArray.get_Item(0).XYZPoint;
			}
			return result;
		}

		public bool PipeConnectHSprinklersElbow(ref Pipe pipe, FamilyInstance sprinklers)
		{
			bool flag = false;
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				Document document = pipe.Document;
				Connector hsprinklersConnector = this.GetHSprinklersConnector(sprinklers);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<XYZ> list = new List<XYZ>();
				list.Add(hsprinklersConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(hsprinklersConnector));
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				Curve curve = document.CreatYJKLine(list[0], list[1], true);
				Curve curve2 = document.CreatYJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin, true);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				if (curveIntersection != null && !hsprinklersConnector.IsConnected)
				{
					Pipe pipe2 = YJKRevitTools.NewPipe(pipe, curveIntersection, hsprinklersConnector.Origin);
					this.PipeSetParameter(hsprinklersConnector, pipe2);
					YJKRevitTools.ConnectTwoElement(hsprinklersConnector, pipe2);
					document.Regenerate();
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
					Connector appropriateConnector = hypiping.GetAppropriateConnector(curveIntersection);
					Connector appropriateConnector2 = hypiping2.GetAppropriateConnector(curveIntersection);
					if (appropriateConnector != null && appropriateConnector2 != null && !appropriateConnector.IsConnected && !appropriateConnector2.IsConnected)
					{
						if (appropriateConnector.Radius == appropriateConnector2.Radius)
						{
							document.Create.NewElbowFitting(appropriateConnector, appropriateConnector2);
						}
						else
						{
							RoutingPreferenceManager routingPreferenceManager = pipe.PipeType.RoutingPreferenceManager;
                            RoutingPreferenceRuleGroupType routingPreferenceRuleGroupType = (RoutingPreferenceRuleGroupType)1;
							ElementId meppartId = routingPreferenceManager.GetRule(routingPreferenceRuleGroupType, 0).MEPPartId;
							FamilySymbol fs = pipe.Document.GetElementById(meppartId) as FamilySymbol;
							this.HYNewElbowFitting(hypiping, hypiping2, fs, curveIntersection);
						}
						document.Regenerate();
						flag = true;
					}
				}
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				flag = false;
				result = flag;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public FamilyInstance HYNewElbowFitting(YJKPiping<Pipe> myPipe, YJKPiping<Pipe> myPipe2, FamilySymbol fs, XYZ intersectionPoint)
		{
			Document document = myPipe.Piping.Document;
			Level level = document.GetElementById(myPipe.Piping.LevelId) as Level;
			UnifiedModified.ActivateFamilySymbol(fs);
			FamilyInstance familyInstance = document.Create.NewFamilyInstance(intersectionPoint, fs, level, (Autodesk.Revit.DB.Structure.StructuralType)0);
			document.Regenerate();
			if ((familyInstance.Location as LocationPoint).Point.DistanceTo(intersectionPoint) > 0.001)
			{
				XYZ xyz = intersectionPoint - (familyInstance.Location as LocationPoint).Point;
				ElementTransformUtils.MoveElement(document, familyInstance.Id, xyz);
			}
			this.SettingElbowParameter(familyInstance, myPipe, myPipe2);
			this.RotateElbow(familyInstance, myPipe, myPipe2, intersectionPoint);
			document.Regenerate();
			return familyInstance;
		}

		private void SettingElbowParameter(FamilyInstance elbowFi, YJKPiping<Pipe> myPipe, YJKPiping<Pipe> myPipe2)
		{
			Parameter parameter = myPipe.Piping.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM);
			if (parameter != null)
			{
				if (elbowFi.GetParameter("公称半径") != null && !elbowFi.GetParameter("公称半径").IsReadOnly)
				{
					elbowFi.GetParameter("公称半径").Set(parameter.AsDouble() / 2.0);
				}
				else if (elbowFi.GetParameter("公称直径") != null && !elbowFi.GetParameter("公称直径").IsReadOnly)
				{
					elbowFi.GetParameter("公称直径").Set(parameter.AsDouble());
				}
				else if (elbowFi.GetParameter("入口直径") != null && !elbowFi.GetParameter("入口直径").IsReadOnly)
				{
					elbowFi.GetParameter("入口直径").Set(parameter.AsDouble());
				}
				IList<Connector> appropriateConnector = myPipe.GetAppropriateConnector(myPipe2.Piping);
				double num = 0.0;
				if (appropriateConnector != null && appropriateConnector.Count > 1)
				{
					if (appropriateConnector[0].CoordinateSystem.BasisZ.IsAlmostEqualTo(-appropriateConnector[1].CoordinateSystem.BasisZ))
					{
						num = 0.0;
					}
					else
					{
						num = appropriateConnector[0].CoordinateSystem.BasisZ.AngleTo(-appropriateConnector[1].CoordinateSystem.BasisZ);
					}
				}
				if (num > 0.0001)
				{
					Parameter parameter2 = elbowFi.GetParameter("角度");
					if (parameter2 != null && !parameter2.IsReadOnly)
					{
						parameter2.Set(num);
						return;
					}
					if (Math.Abs(num - Math.PI*.5) > 0.0001)
					{
						throw new Exception("管件没有角度或角度为只读，请选择正确的管件");
					}
				}
				return;
			}
			throw new Exception("水管半径未找到");
		}

		private void RotateElbow(FamilyInstance elbowFi, YJKPiping<Pipe> myPipe, YJKPiping<Pipe> myPipe2, XYZ intersectionPoint)
		{
			Connector teeMainConnector = new ConnectHelper().GetTeeMainConnector(elbowFi);
			Connector connector = null;
			ConnectorSetIterator connectorSetIterator = elbowFi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
			while (connectorSetIterator.MoveNext())
			{
				object obj = connectorSetIterator.Current;
				Connector connector2 = obj as Connector;
				if (!connector2.Origin.IsAlmostEqualTo(teeMainConnector.Origin))
				{
					connector = connector2;
				}
			}
			Connector appropriateConnector = myPipe.GetAppropriateConnector(intersectionPoint);
			myPipe2.GetAppropriateConnector(intersectionPoint);
			Connector distanceConnector = myPipe2.GetDistanceConnector(intersectionPoint);
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			XYZ xyz = appropriateConnector.CoordinateSystem.BasisZ.CrossProduct(-teeMainConnector.CoordinateSystem.BasisZ).Normalize();
			double degree = 360.0 - YJKRevitTools.GetTwoVectorAngle(appropriateConnector.CoordinateSystem.BasisZ, -teeMainConnector.CoordinateSystem.BasisZ, xyz) * 180.0 / Math.PI;
			if (xyz.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0)))
			{
				xyz = new XYZ(0.0, 0.0, 1.0);
			}
			hyrotateFamilyInstance.RotateElem(elbowFi, intersectionPoint, xyz, degree);
			xyz = (myPipe.StartConnector.Origin - myPipe.EndConnector.Origin).Normalize();
			XYZ xyz2 = distanceConnector.CoordinateSystem.BasisZ.CrossProduct(connector.CoordinateSystem.BasisZ).Normalize();
			int num = 0;
			while (!this.IsSameDirection(distanceConnector, connector) && num < 3)
			{
				degree = YJKRevitTools.GetTwoVectorAngle(distanceConnector.CoordinateSystem.BasisZ, connector.CoordinateSystem.BasisZ, xyz2) * 180.0 / Math.PI;
				if (xyz2.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0)))
				{
					degree = 180.0;
				}
				hyrotateFamilyInstance.RotateElem(elbowFi, intersectionPoint, xyz, degree);
				num++;
			}
			myPipe.Extend(teeMainConnector);
			myPipe2.Extend(connector);
		}

		private bool IsSameDirection(Connector conn1, Connector conn2)
		{
			bool result = true;
			if (conn1 == null || conn2 == null)
			{
				return false;
			}
			double num = conn1.CoordinateSystem.BasisZ.AngleTo(conn2.CoordinateSystem.BasisZ);
			if (!conn1.CoordinateSystem.BasisZ.IsAlmostEqualTo(conn2.CoordinateSystem.BasisZ) || num > 0.001)
			{
				result = false;
			}
			return result;
		}

		private void PipeSetParameter(Connector connector, Pipe pipe)
		{
			if (connector.Shape == ConnectorSharp.Rect)
			{
				double height = connector.Height;
				double width = connector.Width;
				Parameter parameter = connector.Owner.GetParameter(BuiltInParameter.FAMILY_LEVEL_PARAM);
				if (parameter != null)
				{
					parameter.AsElementId();
				}
				else
				{
					parameter = connector.Owner.GetParameter(BuiltInParameter.INSTANCE_SCHEDULE_ONLY_LEVEL_PARAM);
					parameter.AsElementId();
				}
				pipe.GetParameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM).Set(height);
				pipe.GetParameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM).Set(width);
				Parameter parameter2 = pipe.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM);
				return;
			}
			if (connector.Shape == ConnectorSharp.Round)
			{
				double radius = connector.Radius;
				Parameter parameter3 = connector.Owner.GetParameter(BuiltInParameter.FAMILY_LEVEL_PARAM);
				if (parameter3 != null)
				{
					parameter3.AsElementId();
				}
				else
				{
					parameter3 = connector.Owner.GetParameter(BuiltInParameter.INSTANCE_SCHEDULE_ONLY_LEVEL_PARAM);
					parameter3.AsElementId();
				}
				pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(radius * 2.0);
				Parameter parameter4 = pipe.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM);
			}
		}

		private static double mmToFeet(double val)
		{
			return val / 304.8;
		}

		public bool PipeConnectHSprinklersTakeOff(Pipe pipe, FamilyInstance sprinklers)
		{
			bool flag = false;
			bool result;
			try
			{
				Document document = pipe.Document;
				Connector hsprinklersConnector = this.GetHSprinklersConnector(sprinklers);
				IList<XYZ> list = new List<XYZ>();
				list.Add(hsprinklersConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(hsprinklersConnector));
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				Curve curve = Line.CreateBound(list[0], list[1]);
				Curve curve2 = Line.CreateBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				if (curveIntersection != null)
				{
					Pipe pipe2 = YJKRevitTools.NewPipe(pipe, curveIntersection, hsprinklersConnector.Origin);
					this.PipeSetParameter(hsprinklersConnector, pipe2);
					YJKRevitTools.ConnectTwoElement(hsprinklersConnector, pipe2);
					document.Regenerate();
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
					document.Create.NewTakeoffFitting(hypiping2.GetAppropriateConnector(curveIntersection), hypiping.Piping);
					flag = true;
				}
				else
				{
					int num = 1;
					IList<XYZ> pipeHSprinklersPoint = this.GetPipeHSprinklersPoint(list[0], list[1], hypiping, ref num);
					if (pipeHSprinklersPoint != null && pipeHSprinklersPoint.Count > 1)
					{
						Pipe pipe3 = YJKRevitTools.NewPipe(pipe, pipeHSprinklersPoint[1], hsprinklersConnector.Origin);
						this.PipeSetParameter(hsprinklersConnector, pipe3);
						YJKRevitTools.ConnectTwoElement(hsprinklersConnector, pipe3);
						Pipe pipe4 = YJKRevitTools.NewPipe(hypiping.Piping, pipeHSprinklersPoint[0], pipeHSprinklersPoint[1]);
						this.PipeSetParameter(hsprinklersConnector, pipe4);
						document.Regenerate();
						YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipe3);
						YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipe4);
						document.Create.NewElbowFitting(hypiping3.GetAppropriateConnector(pipeHSprinklersPoint[1]), hypiping4.GetAppropriateConnector(pipeHSprinklersPoint[1]));
						document.Create.NewTakeoffFitting(hypiping4.GetAppropriateConnector(pipeHSprinklersPoint[0]), hypiping.Piping);
						flag = true;
					}
				}
				result = flag;
			}
			catch (Exception)
			{
				flag = false;
				result = flag;
			}
			return result;
		}

		public Pipe PipeConnectHSprinklersTakeOff(Pipe pipe, FamilyInstance sprinklers, bool connectBatch)
		{
			Pipe pipe2 = null;
			Pipe result;
			try
			{
				Document document = pipe.Document;
				Connector hsprinklersConnector = this.GetHSprinklersConnector(sprinklers);
				IList<XYZ> list = new List<XYZ>();
				list.Add(hsprinklersConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(hsprinklersConnector));
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				Curve curve = Line.CreateBound(list[0], list[1]);
				Curve curve2 = Line.CreateBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				if (curveIntersection != null)
				{
					Pipe pipe3 = YJKRevitTools.NewPipe(pipe, curveIntersection, hsprinklersConnector.Origin);
					this.PipeSetParameter(hsprinklersConnector, pipe3);
					YJKRevitTools.ConnectTwoElement(hsprinklersConnector, pipe3);
					document.Regenerate();
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe3);
					document.Create.NewTakeoffFitting(hypiping2.GetAppropriateConnector(curveIntersection), hypiping.Piping);
					pipe2 = hypiping2.Piping;
				}
				else
				{
					int num = 1;
					IList<XYZ> pipeHSprinklersPoint = this.GetPipeHSprinklersPoint(list[0], list[1], hypiping, ref num);
					if (pipeHSprinklersPoint != null && pipeHSprinklersPoint.Count > 1)
					{
						Pipe pipe4 = YJKRevitTools.NewPipe(pipe, pipeHSprinklersPoint[1], hsprinklersConnector.Origin);
						this.PipeSetParameter(hsprinklersConnector, pipe4);
						YJKRevitTools.ConnectTwoElement(hsprinklersConnector, pipe4);
						Pipe pipe5 = YJKRevitTools.NewPipe(hypiping.Piping, pipeHSprinklersPoint[0], pipeHSprinklersPoint[1]);
						this.PipeSetParameter(hsprinklersConnector, pipe5);
						document.Regenerate();
						YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipe4);
						YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipe5);
						document.Create.NewElbowFitting(hypiping3.GetAppropriateConnector(pipeHSprinklersPoint[1]), hypiping4.GetAppropriateConnector(pipeHSprinklersPoint[1]));
						document.Create.NewTakeoffFitting(hypiping4.GetAppropriateConnector(pipeHSprinklersPoint[0]), hypiping.Piping);
						pipe2 = hypiping4.Piping;
					}
				}
				result = pipe2;
			}
			catch (Exception)
			{
				pipe2 = null;
				result = pipe2;
			}
			return result;
		}

		public bool judgeWhatConnectFunction(IList<Element> listFi)
		{
			int num = 0;
			int num2 = 0;
			foreach (Element element in listFi)
			{
				FamilyInstance familyInstance = element as FamilyInstance;
				if (familyInstance.Category.Id.IntegerValue == -2008099)
				{
					if (YJKRevitTools.GetElementConnector(familyInstance).CoordinateSystem.BasisZ.Z == 0.0)
					{
						num++;
					}
					else
					{
						num2++;
					}
				}
			}
			return num2 < num;
		}

		public bool PipeConnBranchPipe(Autodesk.Revit.DB.Document doc, Pipe pipe, IList<Element> pipeList)
		{
			bool result;
			try
			{
				pipe = this.CheckPipeLength(pipe, pipeList);
				IList<AutoConnectParameter> list = null;
				ConnectHelper connectHelper = new ConnectHelper();
				string a = "";
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				XYZ origin = hypiping.StartConnector.Connector.Origin;
				XYZ origin2 = hypiping.EndConnector.Connector.Origin;
				IList<AutoConnectParameter> listParam = connectHelper.InitHYPipe(pipe, pipeList);
				list = connectHelper.PositionAssign(connectHelper.OrderByDistanceOfEnd(origin, origin2, listParam), origin, origin2);
				FamilySymbol familySymbolByName = YJKRevitTools.GetFamilySymbolByName("Fitting", "水管三通", doc, ref a);
				IList<AutoConnectParameter> list2 = new List<AutoConnectParameter>();
				int num = 1;
				int i = 0;
				int count = list.Count;
				for (i = 0; i < count; i++)
				{
					int j;
					for (j = i; j < count; j++)
					{
						if (list[j].IndexOfDuct != num)
						{
							i = j - 1;
							break;
						}
						list2.Add(list[j]);
					}
					if (i != j - 1)
					{
						i = j - 1;
					}
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(doc.GetElementById(list2[0].ElementId) as Pipe);
					YJKLine hyline = new YJKLine(hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
					bool flag = false;
					for (int k = 0; k < list2.Count; k++)
					{
						YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(doc.GetElementById(list2[k].ElementId) as Pipe);
						if (hyline.IsPointOnCurveLine(hypiping3.StartConnector.Origin) || hyline.IsPointOnCurveLine(hypiping3.EndConnector.Origin))
						{
							XYZ dlineP = YJKRevitTools.GetDLineP(origin, origin2, hypiping3.StartConnector.Origin, hypiping3.EndConnector.Origin);
							if (hypiping3.IsPointOnPipingCurve(dlineP))
							{
								pipeList.Remove(doc.GetElementById(list2[k].ElementId));
								pipe = this.GetNextPipe(origin, hypiping3.Piping, pipe, doc, familySymbolByName, ref a);
								flag = true;
								break;
							}
						}
					}
					if (a != "")
					{
						return false;
					}
					if (!flag)
					{
						int index = 0;
						double num2 = 99999.0;
						Pipe pipe2 = null;
						for (int l = 0; l < list2.Count; l++)
						{
							pipe2 = (doc.GetElementById(list2[l].ElementId) as Pipe);
							YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipe2);
							XYZ point = (hypiping4.StartConnector.Origin + hypiping4.EndConnector.Origin) / 2.0;
							if (hypiping.GetDistanceSumEnd(point) < num2)
							{
								num2 = hypiping.GetDistanceSumEnd(point);
								index = l;
							}
						}
						pipe2 = (doc.GetElementById(list2[index].ElementId) as Pipe);
						List<Connector> pipeConnConnector = YJKRevitTools.GetPipeConnConnector(pipe2);
						List<Element> list3 = new List<Element>();
						foreach (Connector connector in pipeConnConnector)
						{
							Connector connectConnector = YJKRevitTools.GetConnectConnector(connector);
							list3.Add(connectConnector.Owner);
						}
						if (list3.Count != 2)
						{
							pipe = this.GetNextPipe(origin, pipe2, pipe, doc, familySymbolByName, ref a);
							doc.Regenerate();
						}
						else
						{
							Element nearElement = this.GetNearElement(list3, pipe);
							if ((int)((MechanicalFitting)(nearElement as FamilyInstance).MEPModel).PartType == 5)
							{
								List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(nearElement);
								XYZ xyz = null;
								Pipe item = null;
								foreach (Connector connector2 in elementAllConnector)
								{
									foreach (Connector connector3 in pipeConnConnector)
									{
										Connector connectConnector2 = YJKRevitTools.GetConnectConnector(connector3);
										if (connector2.Origin.IsAlmostEqualTo(connectConnector2.Origin))
										{
											xyz = -connectConnector2.CoordinateSystem.BasisZ;
										}
									}
								}
								foreach (Connector connector4 in elementAllConnector)
								{
									if (!connector4.CoordinateSystem.BasisZ.IsAlmostEqualTo(-xyz))
									{
										item = (YJKRevitTools.GetConnectConnector(connector4).Owner as Pipe);
									}
								}
								LocationPoint locationPoint = nearElement.Location as LocationPoint;
								Pipe pipe3 = YJKRevitTools.NewPipe(pipe2, locationPoint.Point, locationPoint.Point + xyz * 100.0 / 304.8);
								doc.DeleteElement(nearElement);
								doc.Regenerate();
								IList<Pipe> list4 = new List<Pipe>();
								list4.Add(pipe2);
								list4.Add(pipe3);
								list4.Add(item);
								this.NewTeeFitting(ref list4, true);
								doc.Regenerate();
								list4.Clear();
								pipe = this.GetNextPipe(origin, pipe3, pipe, doc, familySymbolByName, ref a);
								doc.Regenerate();
							}
							else if ((int)((MechanicalFitting)(nearElement as FamilyInstance).MEPModel).PartType == 6)
							{
								List<Connector> elementAllConnector2 = YJKRevitTools.GetElementAllConnector(nearElement);
								XYZ xyz2 = null;
								IList<Pipe> list5 = new List<Pipe>();
								foreach (Connector connector5 in elementAllConnector2)
								{
									list5.Add(YJKRevitTools.GetConnectConnector(connector5).Owner as Pipe);
									if (!connector5.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0)) && !connector5.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0)))
									{
										xyz2 = connector5.Origin;
									}
								}
								XYZ endXYZ = new XYZ(hypiping.GetProject(xyz2).X, hypiping.GetProject(xyz2).Y, xyz2.Z);
								Pipe pipe4 = YJKRevitTools.NewPipe(pipe2, xyz2, endXYZ);
								list5.Add(pipe4);
								doc.DeleteElement(nearElement);
								pipe = this.GetNextPipe(origin, pipe4, pipe, doc, familySymbolByName, ref a);
								this.NewSomeConnect(list5);
								list5.Clear();
							}
						}
					}
					list2.Clear();
					num++;
				}
				result = true;
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = false;
			}
			return result;
		}

		public List<List<Element>> SprinklerConn(Autodesk.Revit.DB.Document doc, List<Element> listSprinklers, double pipeDistance, Pipe pipe)
		{
			if (listSprinklers.Count < 1)
			{
				return null;
			}
			List<List<Element>> list = new List<List<Element>>();
			Connector elementConnector = YJKRevitTools.GetElementConnector(listSprinklers[0]);
			bool flag = false;
			IList<Element> list2 = new List<Element>();
			Dictionary<Element, Element> dictionary = new Dictionary<Element, Element>();
			if (elementConnector.IsConnected)
			{
				flag = true;
				foreach (Element element in listSprinklers)
				{
					Element owner = YJKRevitTools.GetConnectConnector(YJKRevitTools.GetElementConnector(element)).Owner;
					list2.Add(owner);
					dictionary.Add(owner, element);
				}
			}
			double num = elementConnector.Radius * 2.0;
			pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(num);
			doc.Regenerate();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			IList<AutoConnectParameter> list3 = null;
			XYZ origin = hypiping.StartConnector.Connector.Origin;
			XYZ origin2 = hypiping.EndConnector.Connector.Origin;
			ConnectHelper connectHelper = new ConnectHelper();
			IList<AutoConnectParameter> listParam;
			if (!flag)
			{
				listParam = connectHelper.InitYJKElement(pipe, listSprinklers);
			}
			else
			{
				listParam = connectHelper.InitHYPipe(pipe, list2);
			}
			list3 = connectHelper.PositionAssign(connectHelper.OrderByDistanceOfEnd(origin, origin2, listParam), origin, origin2);
			XYZ basisZ = elementConnector.CoordinateSystem.BasisZ;
			IList<AutoConnectParameter> list4 = new List<AutoConnectParameter>();
			int num2 = 1;
			int i = 0;
			int count = list3.Count;
			string text = "";
			FamilySymbol familySymbolByName = YJKRevitTools.GetFamilySymbolByName("Fitting", "水管三通", doc, ref text);
			for (i = 0; i < count; i++)
			{
				int j;
				for (j = i; j < count; j++)
				{
					if (list3[j].IndexOfDuct != num2)
					{
						i = j - 1;
						break;
					}
					list4.Add(list3[j]);
				}
				if (i != j - 1)
				{
					i = j - 1;
				}
				if (list4.Count > 1)
				{
					if (!connectHelper.IsElementAlign(list4))
					{
						List<Element> list5 = new List<Element>();
						foreach (AutoConnectParameter autoConnectParameter in list4)
						{
							list5.Add(doc.GetElementById(autoConnectParameter.ElementId));
						}
					}
					IList<Element> list6 = new List<Element>();
					double num3 = 0.0;
					XYZ xyz = null;
					XYZ endXYZ = null;
					for (int k = 0; k < list4.Count - 1; k++)
					{
						for (int l = k + 1; l < list4.Count; l++)
						{
							if (!flag)
							{
								double num4 = YJKRevitTools.DistanceTwoXYZ(list4[k].LocationPoint.Point, list4[l].LocationPoint.Point);
								if (num4 > num3)
								{
									num3 = num4;
									xyz = list4[k].LocationPoint.Point + pipeDistance * 1000.0 / 304.8 * basisZ;
									endXYZ = list4[l].LocationPoint.Point + pipeDistance * 1000.0 / 304.8 * basisZ;
								}
							}
							else
							{
								double num4 = YJKRevitTools.DistanceTwoXYZ(list4[k].PipePoint, list4[l].PipePoint);
								if (num4 > num3)
								{
									num3 = num4;
									xyz = list4[k].PipePoint;
									endXYZ = list4[l].PipePoint;
								}
							}
						}
					}
					if (xyz != null)
					{
						Pipe pipe2 = YJKRevitTools.NewPipe(pipe, xyz, endXYZ);
						YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
						hypiping2.SetDiameter(num * 304.8);
						XYZ origin3 = hypiping2.StartConnector.Origin;
						XYZ origin4 = hypiping2.EndConnector.Origin;
						doc.Regenerate();
						foreach (AutoConnectParameter autoConnectParameter2 in list4)
						{
							list6.Add(doc.GetElementById(autoConnectParameter2.ElementId));
						}
						list.Add(list6.ToList<Element>());
						list4.Clear();
						if (!flag)
						{
							this.PipeConnectHSprinklersByRectangle(pipe2, list6, 1);
						}
						else
						{
							listParam = connectHelper.InitHYPipe(pipe2, list6);
							list6.Clear();
							IList<AutoConnectParameter> list7 = connectHelper.PositionAssign(connectHelper.OrderByDistanceOfEnd(origin3, origin4, listParam), origin3, origin4);
							for (int m = 0; m < list7.Count; m++)
							{
								text = "temp";
								Pipe riserPipe = doc.GetElementById(list7[m].ElementId) as Pipe;
								pipe2 = this.GetNextPipe(origin3, riserPipe, pipe2, doc, familySymbolByName, ref text);
							}
						}
					}
				}
				list4.Clear();
				num2++;
			}
			if (flag)
			{
				List<List<Element>> list8 = new List<List<Element>>();
				foreach (List<Element> list9 in list)
				{
					List<Element> list10 = new List<Element>();
					foreach (Element key in list9)
					{
						Element element2 = null;
						if (dictionary.TryGetValue(key, out element2) && element2 != null)
						{
							list10.Add(element2);
						}
					}
					list8.Add(list10);
				}
				list = list8;
			}
			doc.DeleteElement(pipe);
			doc.Regenerate();
			return list;
		}

		public List<List<Element>> SprinklerCeConn(Autodesk.Revit.DB.Document doc, List<Element> listSprinklers, double pipeDistance, Pipe pipe, int sprinklerConnType)
		{
			if (listSprinklers.Count < 1)
			{
				return null;
			}
			List<List<Element>> list = new List<List<Element>>();
			Connector elementConnector = YJKRevitTools.GetElementConnector(listSprinklers[0]);
			double num = elementConnector.Radius * 2.0;
			pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(num);
			doc.Regenerate();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			IList<AutoConnectParameter> list2 = null;
			XYZ origin = hypiping.StartConnector.Connector.Origin;
			XYZ origin2 = hypiping.EndConnector.Connector.Origin;
			ConnectHelper connectHelper = new ConnectHelper();
			IList<AutoConnectParameter> listParam = connectHelper.InitYJKElement(pipe, listSprinklers);
			list2 = connectHelper.PositionAssign(connectHelper.OrderByDistanceOfEnd(origin, origin2, listParam), origin, origin2);
			XYZ basisZ = elementConnector.CoordinateSystem.BasisZ;
			IList<AutoConnectParameter> list3 = new List<AutoConnectParameter>();
			XYZ basisZ2 = hypiping.StartConnector.Connector.CoordinateSystem.BasisZ;
			XYZ basisZ3 = hypiping.EndConnector.Connector.CoordinateSystem.BasisZ;
			int num2 = 1;
			int i = 0;
			int count = list2.Count;
			string text = "";
			FamilySymbol familySymbolByName = YJKRevitTools.GetFamilySymbolByName("Fitting", "水管三通", doc, ref text);
			for (i = 0; i < count; i++)
			{
				int j;
				for (j = i; j < count; j++)
				{
					if (list2[j].IndexOfDuct != num2)
					{
						i = j - 1;
						break;
					}
					list3.Add(list2[j]);
				}
				if (i != j - 1)
				{
					i = j - 1;
				}
				if (list3.Count > 1)
				{
					if (!connectHelper.IsElementAlign(list3))
					{
						List<Element> list4 = new List<Element>();
						foreach (AutoConnectParameter autoConnectParameter in list3)
						{
							list4.Add(doc.GetElementById(autoConnectParameter.ElementId));
						}
						connectHelper.AlignElement(origin, origin2, list4);
					}
					IList<Element> list5 = new List<Element>();
					double num3 = 0.0;
					XYZ xyz = null;
					XYZ endXYZ = null;
					for (int k = 0; k < list3.Count - 1; k++)
					{
						XYZ xyz2 = this.PipeVector(pipeDistance * 1000.0, sprinklerConnType);
						for (int l = k + 1; l < list3.Count; l++)
						{
							double num4 = YJKRevitTools.DistanceTwoXYZ(list3[k].LocationPoint.Point, list3[l].LocationPoint.Point);
							if (num4 > num3)
							{
								num3 = num4;
								xyz = list3[k].LocationPoint.Point + xyz2;
								endXYZ = list3[l].LocationPoint.Point + xyz2;
							}
						}
					}
					if (xyz != null)
					{
						Pipe pipe2 = YJKRevitTools.NewPipe(pipe, xyz, endXYZ);
						YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
						XYZ origin3 = hypiping2.StartConnector.Origin;
						XYZ origin4 = hypiping2.EndConnector.Origin;
						hypiping2.SetDiameter(num * 304.8);
						doc.Regenerate();
						foreach (AutoConnectParameter autoConnectParameter2 in list3)
						{
							list5.Add(doc.GetElementById(autoConnectParameter2.ElementId));
						}
						list.Add(list5.ToList<Element>());
						list3.Clear();
						foreach (Element element in list5)
						{
							Connector elementConnector2 = YJKRevitTools.GetElementConnector(element);
							double degree = YJKRevitTools.GetTwoXYVectorAngle(elementConnector2.CoordinateSystem.BasisZ, hypiping2.GetXYVectorPointToPiping(elementConnector2.Origin)) * 180.0 / Math.PI;
							new YJKRotateFamilyInstance().RotateElem(doc, element as FamilyInstance, degree);
							doc.Regenerate();
						}
						listParam = connectHelper.InitYJKElement(pipe2, list5);
						IList<AutoConnectParameter> list6 = connectHelper.PositionAssign(connectHelper.OrderByDistanceOfEnd(origin3, origin4, listParam), origin3, origin4);
						for (int m = 0; m < list6.Count; m++)
						{
							Element elementById = doc.GetElementById(list6[m].ElementId);
							Pipe riserPipe = this.ConnCeSprinkler(elementById, pipe, hypiping2);
							pipe2 = this.GetNextPipe(origin3, riserPipe, pipe2, doc, familySymbolByName, ref text);
							doc.Regenerate();
						}
					}
				}
				list3.Clear();
				num2++;
			}
			doc.DeleteElement(pipe);
			doc.Regenerate();
			return list;
		}

		public bool ConnSprinklersInALine(Autodesk.Revit.DB.Document doc, IList<Element> listSprinklers, double distance, int nPipeType)
		{
			bool result;
			try
			{
				if (distance == 0.0)
				{
					result = true;
				}
				else
				{
					double num = 0.0;
					Connector elementConnector = YJKRevitTools.GetElementConnector(listSprinklers[0]);
					XYZ basisZ = elementConnector.CoordinateSystem.BasisZ;
					double num2 = elementConnector.Radius * 2.0;
					XYZ xyz = null;
					XYZ xyz2 = null;
					PipingSystemType pipingSystemType = null;
					foreach (Element element in YJKElementFilter.GetElementByClass(typeof(PipingSystemType), doc))
					{
						pipingSystemType = (element as PipingSystemType);
						if ((int)pipingSystemType.SystemClassification == 23)
						{
							break;
						}
					}
					PipeType pipeType = YJKElementFilter.GetElementByClass(typeof(PipeType), doc)[nPipeType] as PipeType;
					for (int i = 0; i < listSprinklers.Count - 1; i++)
					{
						for (int j = i + 1; j < listSprinklers.Count; j++)
						{
							LocationPoint locationPoint = listSprinklers[i].Location as LocationPoint;
							LocationPoint locationPoint2 = listSprinklers[j].Location as LocationPoint;
							double num3 = YJKRevitTools.DistanceTwoXYZ(locationPoint.Point, locationPoint2.Point);
							if (num3 > num)
							{
								num = num3;
								xyz = locationPoint.Point + distance * 1000.0 / 304.8 * basisZ;
								xyz2 = locationPoint2.Point + distance * 1000.0 / 304.8 * basisZ;
							}
						}
					}
					if (listSprinklers.Count > 1)
					{
						Pipe pipe = Pipe.Create(doc, pipingSystemType.Id, pipeType.Id, doc.ActiveView.GenLevel.Id, xyz, xyz2);
						new YJKPiping<Pipe>(pipe).SetDiameter(num2 * 304.8);
						doc.Regenerate();
						this.PipeConnectHSprinklersByRectangle(pipe, listSprinklers, 1);
					}
					result = true;
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = false;
			}
			return result;
		}

		public bool PipeConnectCeSprinkler(IList<Element> listElment, Pipe pipe)
		{
			bool flag = true;
			double pipeDistance = 100.0;
			IList<AutoConnectParameter> list = null;
			Document document = pipe.Document;
			bool result;
			try
			{
				pipe = this.CheckPipeLength(pipe, listElment);
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				XYZ origin = hypiping.StartConnector.Connector.Origin;
				XYZ origin2 = hypiping.EndConnector.Connector.Origin;
				ConnectHelper connectHelper = new ConnectHelper();
				IList<AutoConnectParameter> listParam = connectHelper.InitYJKElement(pipe, listElment);
				list = connectHelper.PositionAssign(connectHelper.OrderByDistanceOfEnd(origin, origin2, listParam), origin, origin2);
				double diameter = YJKRevitTools.GetElementConnector(listElment[0]).Radius * 2.0;
				string text = "";
				FamilySymbol familySymbolByName = YJKRevitTools.GetFamilySymbolByName("Fitting", "水管三通", document, ref text);
				IList<AutoConnectParameter> list2 = new List<AutoConnectParameter>();
				XYZ basisZ = hypiping.StartConnector.Connector.CoordinateSystem.BasisZ;
				XYZ basisZ2 = hypiping.EndConnector.Connector.CoordinateSystem.BasisZ;
				int num = 1;
				int i = 0;
				for (i = 0; i < list.Count; i++)
				{
					int j;
					for (j = i; j < list.Count; j++)
					{
						if (list[j].IndexOfDuct != num)
						{
							i = j - 1;
							break;
						}
						list2.Add(list[j]);
					}
					if (i != j - 1)
					{
						i = j - 1;
					}
					if (list2.Count > 1)
					{
						if (!connectHelper.IsElementAlign(list2))
						{
							List<Element> list3 = new List<Element>();
							foreach (AutoConnectParameter autoConnectParameter in list2)
							{
								list3.Add(document.GetElementById(autoConnectParameter.ElementId));
							}
							connectHelper.AlignElement(origin, origin2, list3);
						}
						IList<Element> list4 = new List<Element>();
						double num2 = 0.0;
						XYZ xyz = null;
						XYZ xyz2 = null;
						for (int k = 0; k < list2.Count - 1; k++)
						{
							XYZ xyz3 = this.PipeVector(document.GetElementById(list2[k].ElementId), basisZ, basisZ2, pipeDistance);
							for (int l = k + 1; l < list2.Count; l++)
							{
								double num3 = YJKRevitTools.DistanceTwoXYZ(list2[k].LocationPoint.Point, list2[l].LocationPoint.Point);
								if (num3 > num2)
								{
									num2 = num3;
									xyz = list2[k].LocationPoint.Point + xyz3;
									xyz2 = list2[l].LocationPoint.Point + xyz3;
								}
							}
						}
						if (xyz != null)
						{
							YJKLine hyline = new YJKLine(xyz, xyz2);
							Pipe pipe2 = YJKRevitTools.NewPipe(pipe, xyz, xyz2);
							YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
							this.SetDiameter(pipe2, diameter);
							XYZ origin3 = hypiping2.StartConnector.Origin;
							XYZ origin4 = hypiping2.EndConnector.Origin;
							document.Regenerate();
							foreach (AutoConnectParameter autoConnectParameter2 in list2)
							{
								list4.Add(document.GetElementById(autoConnectParameter2.ElementId));
							}
							listParam = connectHelper.InitYJKElement(pipe2, list4);
							IList<AutoConnectParameter> list5 = connectHelper.PositionAssign(connectHelper.OrderByDistanceOfEnd(origin3, origin4, listParam), origin3, origin4);
							for (int m = 0; m < list5.Count; m++)
							{
								Element elementById = document.GetElementById(list5[m].ElementId);
								Pipe riserPipe = this.ConnCeSprinkler(elementById, pipe, hypiping2);
								pipe2 = this.GetNextPipe(origin3, riserPipe, pipe2, document, familySymbolByName, ref text);
								document.Regenerate();
							}
							bool flag2 = false;
							foreach (Element element in YJKElementFilter.GetElementByClass(typeof(Pipe), document, pipe.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId()))
							{
								YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(element as Pipe);
								if (hyline.IsPointOnLine(hypiping3.StartConnector.Origin) || hyline.IsPointOnLine(hypiping3.EndConnector.Origin))
								{
									XYZ dlineP = YJKRevitTools.GetDLineP(origin, origin2, hypiping3.StartConnector.Origin, hypiping3.EndConnector.Origin);
									if (hypiping3.IsPointOnPipingCurve(dlineP))
									{
										pipe = this.GetNextPipe(origin, hypiping3.Piping, pipe, document, familySymbolByName, ref text);
										flag2 = true;
										break;
									}
								}
							}
							if (!flag2)
							{
								XYZ pipeVector = this.PipeVector(document.GetElementById(list2[0].ElementId), basisZ, basisZ2, pipeDistance);
								pipe = this.ConnOneSideSprinkler(document, hypiping, pipe, list2, xyz, origin, pipeVector);
							}
						}
					}
					else
					{
						XYZ pipeVector2 = this.PipeVector(document.GetElementById(list2[0].ElementId), basisZ, basisZ2, pipeDistance);
						pipe = this.ConnSprinklerOne(document, hypiping, pipe, list2, origin, origin2, pipeVector2);
					}
					document.Regenerate();
					list2.Clear();
					num++;
				}
				result = flag;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private Pipe ConnCeSprinkler(Element elemSprinkler, Pipe pipe, YJKPiping<Pipe> hyBranchPipe)
		{
			double diameter = YJKRevitTools.GetElementConnector(elemSprinkler).Radius * 2.0;
			LocationPoint locationPoint = elemSprinkler.Location as LocationPoint;
			XYZ xyz = locationPoint.Point + YJKRevitTools.GetElementConnector(elemSprinkler).CoordinateSystem.BasisZ / 304.8 * 50.0;
			Pipe pipe2 = YJKRevitTools.NewPipe(pipe, locationPoint.Point, xyz);
			this.SetDiameter(pipe2, diameter);
			YJKRevitTools.ConnectTwoElement(elemSprinkler, pipe2);
			Pipe pipe3 = YJKRevitTools.NewPipe(pipe, xyz, hyBranchPipe.GetLineProject(xyz));
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe3);
			this.SetDiameter(pipe3, diameter);
			if (!this.NewElbowFitting(pipe3, pipe2))
			{
				hypiping.Combine(pipe2);
			}
			return pipe3;
		}

		private bool NewElbowFitting(Pipe pipe1, Pipe pipe2)
		{
			return this.NewElbowFitting(new List<Pipe>
			{
				pipe1,
				pipe2
			}, true);
		}

		private XYZ PipeVector(double pipeDistance, int sprinklerConnType)
		{
			if (sprinklerConnType == 1)
			{
				return pipeDistance / 304.8 * new XYZ(0.0, 1.0, 0.0);
			}
			return pipeDistance / 304.8 * new XYZ(1.0, 0.0, 0.0);
		}

		private XYZ PipeVector(Element elem, XYZ pipeStart, XYZ pipeEnd, double pipeDistance)
		{
			XYZ basisZ = YJKRevitTools.GetElementConnector(elem).CoordinateSystem.BasisZ;
			if (basisZ.AngleTo(pipeStart) <= basisZ.AngleTo(pipeEnd))
			{
				return pipeDistance / 304.8 * pipeStart;
			}
			return pipeDistance / 304.8 * pipeEnd;
		}

		private void SetDiameter(Pipe branchPipe, double diameter)
		{
			new YJKPiping<Pipe>(branchPipe).SetDiameter(diameter * 304.8);
		}

		public bool ConnUpDownSprinkler(IList<Pipe> listPipes, Pipe pipe)
		{
			bool flag = true;
			IList<AutoConnectParameter> list = null;
			bool result;
			try
			{
				string text = "";
				Document document = pipe.Document;
				IList<Element> list2 = new List<Element>();
				foreach (Element item in listPipes)
				{
					list2.Add(item);
				}
				pipe = this.CheckPipeLength(pipe, list2);
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				XYZ origin = hypiping.StartConnector.Connector.Origin;
				XYZ origin2 = hypiping.EndConnector.Connector.Origin;
				ConnectHelper connectHelper = new ConnectHelper();
				IList<AutoConnectParameter> listParam = connectHelper.InitHYPipe(pipe, list2);
				list = connectHelper.PositionAssign(connectHelper.OrderByDistanceOfEnd(origin, origin2, listParam), origin, origin2);
				IList<AutoConnectParameter> list3 = new List<AutoConnectParameter>();
				int num = 1;
				int i = 0;
				int count = list.Count;
				FamilySymbol familySymbolByName = YJKRevitTools.GetFamilySymbolByName("Fitting", "水管三通", document, ref text);
				for (i = 0; i < count; i++)
				{
					int j;
					for (j = i; j < count; j++)
					{
						if (list[j].IndexOfDuct != num)
						{
							i = j - 1;
							break;
						}
						list3.Add(list[j]);
					}
					if (i != j - 1)
					{
						i = j - 1;
					}
					if (list3.Count > 1)
					{
						if (!connectHelper.IsElementAlign(list3))
						{
							List<Element> list4 = new List<Element>();
							foreach (AutoConnectParameter autoConnectParameter in list3)
							{
								list4.Add(document.GetElementById(autoConnectParameter.ElementId));
							}
							connectHelper.AlignElement(origin, origin2, list4);
						}
						IList<Element> list5 = new List<Element>();
						double num2 = 0.0;
						XYZ xyz = null;
						XYZ xyz2 = null;
						for (int k = 0; k < list3.Count - 1; k++)
						{
							for (int l = k + 1; l < list3.Count; l++)
							{
								double num3 = YJKRevitTools.DistanceTwoXYZ(list3[k].PipePoint, list3[l].PipePoint);
								if (num3 > num2)
								{
									num2 = num3;
									xyz = list3[k].PipePoint;
									xyz2 = list3[l].PipePoint;
								}
							}
						}
						if (xyz != null)
						{
							YJKLine hyline = new YJKLine(xyz, xyz2);
							Pipe pipe2 = YJKRevitTools.NewPipe(pipe, xyz, xyz2);
							YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
							XYZ origin3 = hypiping2.StartConnector.Origin;
							XYZ origin4 = hypiping2.EndConnector.Origin;
							foreach (AutoConnectParameter autoConnectParameter2 in list3)
							{
								list5.Add(document.GetElementById(autoConnectParameter2.ElementId));
							}
							Pipe pipe3 = this.GetNearElement(list5, pipe) as Pipe;
							YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipe3);
							double num4 = pipe3.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
							hypiping2.SetDiameter(num4 * 304.8);
							Connector connectConnector = YJKRevitTools.GetConnectConnector(YJKRevitTools.GetPipeConnConnector(pipe3)[0]);
							listParam = connectHelper.InitHYPipe(pipe2, list5);
							list5.Clear();
							IList<AutoConnectParameter> list6 = connectHelper.PositionAssign(connectHelper.OrderByDistanceOfEnd(origin3, origin4, listParam), origin3, origin4);
							for (int m = 0; m < list6.Count; m++)
							{
								text = "temp";
								Pipe riserPipe = document.GetElementById(list6[m].ElementId) as Pipe;
								pipe2 = this.GetNextPipe(origin3, riserPipe, pipe2, document, familySymbolByName, ref text);
							}
							bool flag2 = false;
							foreach (Element element in YJKElementFilter.GetElementByClass(typeof(Pipe), document, pipe.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId()))
							{
								YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(element as Pipe);
								if (hyline.IsPointOnLine(hypiping4.StartConnector.Origin) || hyline.IsPointOnLine(hypiping4.EndConnector.Origin))
								{
									XYZ dlineP = YJKRevitTools.GetDLineP(origin, origin2, hypiping4.StartConnector.Origin, hypiping4.EndConnector.Origin);
									if (hypiping4.IsPointOnPipingCurve(dlineP))
									{
										pipe = this.GetNextPipe(origin, hypiping4.Piping, pipe, document, familySymbolByName, ref text);
										flag2 = true;
										break;
									}
								}
							}
							if (!flag2)
							{
								List<Connector> pipeConnConnector = YJKRevitTools.GetPipeConnConnector(YJKRevitTools.GetConnectConnector(connectConnector).Owner as Pipe);
								IList<Pipe> list7 = new List<Pipe>();
								foreach (Connector connector in pipeConnConnector)
								{
									if (!connectConnector.Origin.IsAlmostEqualTo(YJKRevitTools.GetConnectConnector(connector).Origin))
									{
										Element owner = YJKRevitTools.GetConnectConnector(connector).Owner;
										foreach (Connector connector2 in YJKRevitTools.GetElementAllConnector(owner))
										{
											list7.Add(YJKRevitTools.GetConnectConnector(connector2).Owner as Pipe);
										}
										document.DeleteElement(owner);
									}
								}
								XYZ xyz3 = (hypiping3.StartConnector.Origin + hypiping3.EndConnector.Origin) / 2.0;
								XYZ endXYZ = new XYZ(hypiping.GetProject(xyz3).X, hypiping.GetProject(xyz3).Y, xyz3.Z);
								Pipe pipe4 = YJKRevitTools.NewPipe(pipe, xyz3, endXYZ);
								new YJKPiping<Pipe>(pipe4).SetDiameter(num4 * 304.8);
								document.Regenerate();
								list7.Add(pipe4);
								pipe = this.GetNextPipe(origin, pipe4, pipe, document, familySymbolByName, ref text);
								flag = this.NewSomeConnect(list7);
								list7.Clear();
							}
						}
					}
					else
					{
						double num5 = document.GetElementById(list3[0].ElementId).GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
						XYZ pipePoint = list3[0].PipePoint;
						XYZ endXYZ2 = new XYZ(hypiping.GetProject(pipePoint).X, hypiping.GetProject(pipePoint).Y, pipePoint.Z);
						Pipe pipe5 = YJKRevitTools.NewPipe(pipe, pipePoint, endXYZ2);
						new YJKPiping<Pipe>(pipe5).SetDiameter(num5 * 304.8);
						this.GetNextPipe(origin, pipe5, document.GetElementById(list3[0].ElementId) as Pipe, document, familySymbolByName, ref text);
						pipe = this.GetNextPipe(origin, pipe5, pipe, document, familySymbolByName, ref text);
					}
					list3.Clear();
					document.Regenerate();
					num++;
				}
				result = flag;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private Pipe ConnOneSideSprinkler(Autodesk.Revit.DB.Document doc, YJKPiping<Pipe> hypipe, Pipe pipe, IList<AutoConnectParameter> branchParam, XYZ bstartPoint, XYZ startPoint, XYZ pipeVector)
		{
			Pipe pipe2 = null;
			Pipe pipe3 = null;
			string text = "";
			FamilySymbol familySymbolByName = YJKRevitTools.GetFamilySymbolByName("Fitting", "水管三通", doc, ref text);
			double diameter = YJKRevitTools.GetElementConnector(doc.GetElementById(branchParam[0].ElementId) as FamilyInstance).Radius * 2.0;
			IList<Element> list = new List<Element>();
			foreach (AutoConnectParameter autoConnectParameter in branchParam)
			{
				list.Add(doc.GetElementById(autoConnectParameter.ElementId));
			}
			Element nearElement = this.GetNearElement(list, pipe);
			LocationPoint locationPoint = nearElement.Location as LocationPoint;
			Connector elementConnector = YJKRevitTools.GetElementConnector(nearElement);
			if (elementConnector.IsConnected)
			{
				pipe2 = (YJKRevitTools.GetConnectConnector(elementConnector).Owner as Pipe);
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe2);
				Connector connector = (hypiping.StartConnector.Connector.Origin.DistanceTo(elementConnector.Origin) > hypiping.EndConnector.Connector.Origin.DistanceTo(elementConnector.Origin)) ? hypiping.StartConnector.Connector : hypiping.EndConnector.Connector;
				Connector connectConnector = YJKRevitTools.GetConnectConnector(connector);
				if ((int)((MechanicalFitting)(connectConnector.Owner as FamilyInstance).MEPModel).PartType == 5)
				{
					foreach (Connector connector2 in YJKRevitTools.GetElementAllConnector(connectConnector.Owner))
					{
						Connector connectConnector2 = YJKRevitTools.GetConnectConnector(connector2);
						if (connectConnector2.Owner.Id != connector.Owner.Id)
						{
							pipe3 = (connectConnector2.Owner as Pipe);
						}
					}
					doc.DeleteElement(connectConnector.Owner);
					doc.Regenerate();
				}
			}
			XYZ xyz = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, bstartPoint.Z);
			XYZ xyz2 = new XYZ(hypipe.GetProject(xyz).X, hypipe.GetProject(xyz).Y, xyz.Z);
			Pipe pipe4 = YJKRevitTools.NewPipe(pipe, xyz + pipeVector, xyz2 + pipeVector);
			this.SetDiameter(pipe4, diameter);
			doc.Regenerate();
			IList<Pipe> list2 = new List<Pipe>();
			list2.Add(pipe2);
			list2.Add(pipe3);
			list2.Add(pipe4);
			if (!this.NewTeeFitting(ref list2, true))
			{
				list2.Clear();
				List<Connector> pipeConnConnector = YJKRevitTools.GetPipeConnConnector(pipe3);
				Pipe item = null;
				foreach (Connector connector3 in pipeConnConnector)
				{
					Connector connectConnector3 = YJKRevitTools.GetConnectConnector(connector3);
					foreach (Connector connector4 in YJKRevitTools.GetElementAllConnector(connectConnector3.Owner))
					{
						Connector connectConnector4 = YJKRevitTools.GetConnectConnector(connector4);
						if (connectConnector4.Owner.Id != pipe3.Id)
						{
							item = (connectConnector4.Owner as Pipe);
						}
					}
					doc.DeleteElement(connectConnector3.Owner);
					doc.Regenerate();
				}
				this.NewElbowFitting(pipe2, pipe3);
				list2.Add(item);
				list2.Add(pipe3);
				list2.Add(pipe4);
				this.NewTeeFitting(ref list2, true);
				list2.Clear();
			}
			doc.Regenerate();
			pipe = this.GetNextPipe(startPoint, pipe4, pipe, doc, familySymbolByName, ref text);
			doc.Regenerate();
			return pipe;
		}

		private Pipe ConnSprinklerOne(Autodesk.Revit.DB.Document doc, YJKPiping<Pipe> hypipe, Pipe pipe, IList<AutoConnectParameter> branchParam, XYZ startPoint, XYZ endPoint, XYZ pipeVector)
		{
			string text = "";
			FamilySymbol familySymbolByName = YJKRevitTools.GetFamilySymbolByName("Fitting", "水管三通", doc, ref text);
			ConnectHelper connectHelper = new ConnectHelper();
			double diameter = YJKRevitTools.GetElementConnector(doc.GetElementById(branchParam[0].ElementId) as FamilyInstance).Radius * 2.0;
			if (branchParam[0].PositionFlag == 3)
			{
				XYZ point = branchParam[0].LocationPoint.Point;
				XYZ endXYZ = new XYZ(hypipe.GetProject(point).X, hypipe.GetProject(point).Y, point.Z);
				Pipe pipe2 = YJKRevitTools.NewPipe(pipe, point, endXYZ);
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe2);
				this.SetDiameter(pipe2, diameter);
				doc.Regenerate();
				if (!branchParam[0].Connector.IsConnected)
				{
					FamilyInstance fi = doc.GetElementById(branchParam[0].ElementId) as FamilyInstance;
					Connector hsprinklersConnector = this.GetHSprinklersConnector(fi);
					Connector connector = hypiping.StartConnector.Connector;
					Connector connector2 = hypiping.EndConnector.Connector;
					if (hypiping.StartConnector.Direction.IsAlmostEqualTo(-hsprinklersConnector.CoordinateSystem.BasisZ))
					{
						connector.ConnectTo(hsprinklersConnector);
					}
					else if (hypiping.EndConnector.Direction.IsAlmostEqualTo(-hsprinklersConnector.CoordinateSystem.BasisZ))
					{
						connector2.ConnectTo(hsprinklersConnector);
					}
				}
				pipe = this.GetNextPipeing(startPoint, pipe2, pipe, doc, familySymbolByName, ref text);
			}
			else if (branchParam[0].PositionFlag == 1 && branchParam[0].VectorToDuct.GetLength() > 0.0001)
			{
				connectHelper.AlignElement(startPoint, endPoint, doc.GetElementById(branchParam[0].ElementId));
				Element elementById = doc.GetElementById(branchParam[0].ElementId);
				LocationPoint locationPoint = elementById.Location as LocationPoint;
				XYZ xyz = locationPoint.Point + YJKRevitTools.GetElementConnector(elementById).CoordinateSystem.BasisZ / 304.8 * 100.0;
				Pipe pipe3 = YJKRevitTools.NewPipe(pipe, locationPoint.Point, xyz);
				this.SetDiameter(pipe3, diameter);
				YJKRevitTools.ConnectTwoElement(elementById, pipe3);
				Pipe pipe4 = YJKRevitTools.NewPipe(pipe, xyz, xyz + new XYZ(0.0, 0.0, 0.32808398950131235));
				this.SetDiameter(pipe4, diameter);
				doc.Regenerate();
				this.GetNextPipe(startPoint, pipe3, pipe4, doc, familySymbolByName, ref text);
				pipe = this.GetNextPipe(startPoint, pipe4, pipe, doc, familySymbolByName, ref text);
				doc.Regenerate();
			}
			else if (branchParam[0].PositionFlag == 2)
			{
				connectHelper.AlignElement(startPoint, endPoint, doc.GetElementById(branchParam[0].ElementId));
			}
			return pipe;
		}

		private IList<XYZ> GetPipeHSprinklersPoint(XYZ sprinklersOrigin, XYZ sprinklersLocationPoint, YJKPiping<Pipe> hyPipe, ref int conType)
		{
			IList<XYZ> list = new List<XYZ>();
			IntersectionResult intersectionResult = hyPipe.GetLocationCurve().Project(sprinklersLocationPoint);
			Line line = Line.CreateBound(hyPipe.StartConnector.Origin, hyPipe.EndConnector.Origin);
			line.MakeUnbound();
			IntersectionResult intersectionResult2 = line.Project(sprinklersLocationPoint);
			list.Add(intersectionResult.XYZPoint);
			Line line2 = line as Line;
			Line line3 = Line.CreateBound(sprinklersOrigin, sprinklersLocationPoint);
			XYZ xyz = line2.Direction.CrossProduct(line3.Direction);
			Line line4 = Line.CreateBound(intersectionResult.XYZPoint, intersectionResult.XYZPoint + xyz);
			line3.MakeUnbound();
			line4.MakeUnbound();
			IntersectionResultArray intersectionResultArray = new IntersectionResultArray();
			SetComparisonResult setComparisonResult = line3.Intersect(line4, out intersectionResultArray);
			if (4 != (int)setComparisonResult && !intersectionResultArray.IsEmpty)
			{
				list.Add(intersectionResultArray.get_Item(0).XYZPoint);
			}
			XYZ xyzpoint = intersectionResult.XYZPoint;
			if (intersectionResult.XYZPoint.IsAlmostEqualTo(intersectionResult2.XYZPoint))
			{
				if (xyzpoint.IsAlmostEqualTo(hyPipe.StartConnector.Origin) || xyzpoint.IsAlmostEqualTo(hyPipe.EndConnector.Origin) || xyzpoint.DistanceTo(hyPipe.StartConnector.Origin) < 0.01 || xyzpoint.DistanceTo(hyPipe.EndConnector.Origin) < 0.01)
				{
					conType = 0;
				}
				else
				{
					conType = 1;
				}
			}
			else
			{
				conType = 0;
			}
			return list;
		}

		public bool PipeConnectHSprinklersTee(Pipe pipe, FamilyInstance sprinklers)
		{
			bool flag = false;
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				Document document = pipe.Document;
				Connector hsprinklersConnector = this.GetHSprinklersConnector(sprinklers);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				if (hsprinklersConnector != null && hsprinklersConnector.IsConnected)
				{
					throw new Exception("喷头已经有水管连接");
				}
				IList<XYZ> list = new List<XYZ>();
				list.Add(hsprinklersConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(hsprinklersConnector));
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				Curve curve = Line.CreateBound(list[0], list[1]);
				Curve curve2 = Line.CreateBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				if (curveIntersection != null)
				{
					Pipe pipe2 = YJKRevitTools.NewPipe(pipe, curveIntersection, hsprinklersConnector.Origin);
					YJKRevitTools.ConnectTwoElement(hsprinklersConnector, pipe2);
					this.PipeSetParameter(hsprinklersConnector, pipe2);
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
					IList<Pipe> list2 = hypiping.Separate(curveIntersection);
					document.Regenerate();
					if (list2 != null)
					{
						YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(list2[1]);
						document.Create.NewTeeFitting(hypiping.GetAppropriateConnector(curveIntersection), hypiping3.GetAppropriateConnector(curveIntersection), hypiping2.GetAppropriateConnector(curveIntersection));
						flag = true;
					}
					else
					{
						flag = false;
					}
				}
				else
				{
					int num = 1;
					IList<XYZ> pipeHSprinklersPoint = this.GetPipeHSprinklersPoint(list[0], list[1], hypiping, ref num);
					if (pipeHSprinklersPoint != null && pipeHSprinklersPoint.Count > 1)
					{
						Pipe pipe3 = YJKRevitTools.NewPipe(pipe, pipeHSprinklersPoint[1], hsprinklersConnector.Origin);
						this.PipeSetParameter(hsprinklersConnector, pipe3);
						YJKRevitTools.ConnectTwoElement(hsprinklersConnector, pipe3);
						Pipe pipe4 = YJKRevitTools.NewPipe(hypiping.Piping, pipeHSprinklersPoint[0], pipeHSprinklersPoint[1]);
						this.PipeSetParameter(hsprinklersConnector, pipe4);
						document.Regenerate();
						YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipe3);
						YJKPiping<Pipe> hypiping5 = new YJKPiping<Pipe>(pipe4);
						document.Create.NewElbowFitting(hypiping4.GetAppropriateConnector(pipeHSprinklersPoint[1]), hypiping5.GetAppropriateConnector(pipeHSprinklersPoint[1]));
						IList<Pipe> list3 = hypiping.Separate(pipeHSprinklersPoint[0]);
						document.Regenerate();
						if (list3 != null)
						{
							YJKPiping<Pipe> hypiping6 = new YJKPiping<Pipe>(list3[1]);
							document.Create.NewTeeFitting(hypiping.GetAppropriateConnector(pipeHSprinklersPoint[0]), hypiping6.GetAppropriateConnector(pipeHSprinklersPoint[0]), hypiping5.GetAppropriateConnector(pipeHSprinklersPoint[0]));
							flag = true;
						}
						else
						{
							flag = false;
						}
					}
				}
				document.Regenerate();
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public Pipe PipeConnectHSprinklersTee(ref Pipe pipe, FamilyInstance sprinklers, ref XYZ endPoint)
		{
			Pipe pipe2 = null;
			SubTransaction subTransaction = null;
			Pipe result;
			try
			{
				Document document = pipe.Document;
				Connector hsprinklersConnector = this.GetHSprinklersConnector(sprinklers);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<XYZ> list = new List<XYZ>();
				list.Add(hsprinklersConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(hsprinklersConnector));
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				Curve curve = Line.CreateBound(list[0], list[1]);
				Curve curve2 = Line.CreateBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				if (curveIntersection != null)
				{
					Pipe pipe3 = YJKRevitTools.NewPipe(pipe, curveIntersection, hsprinklersConnector.Origin);
					this.PipeSetParameter(hsprinklersConnector, pipe3);
					YJKRevitTools.ConnectTwoElement(hsprinklersConnector, pipe3);
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe3);
					IList<Pipe> list2 = hypiping.Separate(curveIntersection);
					document.Regenerate();
					if (list2 != null)
					{
						YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(list2[1]);
						document.Create.NewTeeFitting(hypiping.GetAppropriateConnector(curveIntersection), hypiping3.GetAppropriateConnector(curveIntersection), hypiping2.GetAppropriateConnector(curveIntersection));
						pipe2 = hypiping2.Piping;
						pipe = this.CheckPipeEndPoint(hypiping, hypiping3, ref endPoint);
					}
					else
					{
						pipe2 = null;
					}
				}
				else
				{
					int num = 1;
					IList<XYZ> pipeHSprinklersPoint = this.GetPipeHSprinklersPoint(list[0], list[1], hypiping, ref num);
					if (pipeHSprinklersPoint != null && pipeHSprinklersPoint.Count > 1)
					{
						Pipe pipe4 = YJKRevitTools.NewPipe(pipe, pipeHSprinklersPoint[1], hsprinklersConnector.Origin);
						this.PipeSetParameter(hsprinklersConnector, pipe4);
						YJKRevitTools.ConnectTwoElement(hsprinklersConnector, pipe4);
						Pipe pipe5 = YJKRevitTools.NewPipe(hypiping.Piping, pipeHSprinklersPoint[0], pipeHSprinklersPoint[1]);
						this.PipeSetParameter(hsprinklersConnector, pipe5);
						document.Regenerate();
						YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipe4);
						YJKPiping<Pipe> hypiping5 = new YJKPiping<Pipe>(pipe5);
						document.Create.NewElbowFitting(hypiping4.GetAppropriateConnector(pipeHSprinklersPoint[1]), hypiping5.GetAppropriateConnector(pipeHSprinklersPoint[1]));
						if (num == 1)
						{
							IList<Pipe> list3 = hypiping.Separate(pipeHSprinklersPoint[0]);
							document.Regenerate();
							if (list3 != null)
							{
								YJKPiping<Pipe> hypiping6 = new YJKPiping<Pipe>(list3[1]);
								document.Create.NewTeeFitting(hypiping.GetAppropriateConnector(pipeHSprinklersPoint[0]), hypiping6.GetAppropriateConnector(pipeHSprinklersPoint[0]), hypiping5.GetAppropriateConnector(pipeHSprinklersPoint[0]));
								pipe2 = hypiping5.Piping;
								pipe = this.CheckPipeEndPoint(hypiping, hypiping6, ref endPoint);
							}
							else
							{
								pipe2 = null;
							}
						}
						else
						{
							document.Create.NewElbowFitting(hypiping.GetAppropriateConnector(pipeHSprinklersPoint[0]), hypiping5.GetAppropriateConnector(pipeHSprinklersPoint[0]));
							pipe2 = hypiping5.Piping;
							pipe = this.CheckPipeEndPoint(hypiping, ref endPoint);
						}
					}
				}
				document.Regenerate();
				subTransaction.Commit();
				result = pipe2;
			}
			catch (Exception)
			{
				pipe2 = null;
				result = pipe2;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool PipeConnectHSprinklersCross(Pipe pipe, FamilyInstance hSprinklers, FamilyInstance hSprinklers2)
		{
			bool flag = false;
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				Document document = pipe.Document;
				Connector hsprinklersConnector = this.GetHSprinklersConnector(hSprinklers);
				Connector hsprinklersConnector2 = this.GetHSprinklersConnector(hSprinklers2);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				if (hsprinklersConnector != null && hsprinklersConnector2 != null && (hsprinklersConnector.IsConnected || hsprinklersConnector2.IsConnected))
				{
					throw new Exception("风口已经有风管连接");
				}
				IList<XYZ> list = new List<XYZ>();
				IList<XYZ> list2 = new List<XYZ>();
				list.Add(hsprinklersConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(hsprinklersConnector));
				list2.Add(hsprinklersConnector2.Origin);
				list2.Add(this.GetConnectorDirectionPoint(hsprinklersConnector2));
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				Curve curve = Line.CreateBound(list[0], list[1]);
				Curve curve2 = Line.CreateBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				Curve curve3 = Line.CreateBound(list2[0], list2[1]);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				curve3.MakeUnbound();
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				XYZ curveIntersection2 = this.GetCurveIntersection(curve3, curve2);
				if (curveIntersection != null && curveIntersection.IsAlmostEqualTo(curveIntersection2))
				{
					Pipe pipe2 = YJKRevitTools.NewPipe(pipe, curveIntersection, hsprinklersConnector.Origin);
					this.PipeSetParameter(hsprinklersConnector, pipe2);
					YJKRevitTools.ConnectTwoElement(hsprinklersConnector, pipe2);
					Pipe pipe3 = YJKRevitTools.NewPipe(pipe, curveIntersection, hsprinklersConnector2.Origin);
					this.PipeSetParameter(hsprinklersConnector2, pipe3);
					YJKRevitTools.ConnectTwoElement(hsprinklersConnector2, pipe3);
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
					YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipe3);
					IList<Pipe> list3 = hypiping.Separate(curveIntersection);
					document.Regenerate();
					if (list3 != null)
					{
						YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(list3[1]);
						document.Create.NewCrossFitting(hypiping.GetAppropriateConnector(curveIntersection), hypiping4.GetAppropriateConnector(curveIntersection), hypiping2.GetAppropriateConnector(curveIntersection), hypiping3.GetAppropriateConnector(curveIntersection));
						flag = true;
					}
					else
					{
						flag = false;
					}
				}
				else if (curveIntersection == null && curveIntersection2 == null)
				{
					int num = 1;
					int num2 = 1;
					IList<XYZ> pipeHSprinklersPoint = this.GetPipeHSprinklersPoint(list[0], list[1], hypiping, ref num);
					IList<XYZ> pipeHSprinklersPoint2 = this.GetPipeHSprinklersPoint(list2[0], list2[1], hypiping, ref num2);
					if (pipeHSprinklersPoint != null && pipeHSprinklersPoint.Count > 1 && pipeHSprinklersPoint2 != null && pipeHSprinklersPoint2.Count > 1 && pipeHSprinklersPoint[0].IsAlmostEqualTo(pipeHSprinklersPoint2[0]))
					{
						Pipe pipe4 = YJKRevitTools.NewPipe(pipe, pipeHSprinklersPoint[1], hsprinklersConnector.Origin);
						this.PipeSetParameter(hsprinklersConnector, pipe4);
						YJKRevitTools.ConnectTwoElement(hsprinklersConnector, pipe4);
						Pipe pipe5 = YJKRevitTools.NewPipe(hypiping.Piping, pipeHSprinklersPoint[0], pipeHSprinklersPoint[1]);
						this.PipeSetParameter(hsprinklersConnector, pipe5);
						Pipe pipe6 = YJKRevitTools.NewPipe(pipe, pipeHSprinklersPoint2[1], hsprinklersConnector2.Origin);
						this.PipeSetParameter(hsprinklersConnector2, pipe6);
						YJKRevitTools.ConnectTwoElement(hsprinklersConnector2, pipe6);
						Pipe pipe7 = YJKRevitTools.NewPipe(hypiping.Piping, pipeHSprinklersPoint2[0], pipeHSprinklersPoint2[1]);
						this.PipeSetParameter(hsprinklersConnector2, pipe7);
						document.Regenerate();
						YJKPiping<Pipe> hypiping5 = new YJKPiping<Pipe>(pipe4);
						YJKPiping<Pipe> hypiping6 = new YJKPiping<Pipe>(pipe5);
						YJKPiping<Pipe> hypiping7 = new YJKPiping<Pipe>(pipe6);
						YJKPiping<Pipe> hypiping8 = new YJKPiping<Pipe>(pipe7);
						document.Create.NewElbowFitting(hypiping5.GetAppropriateConnector(pipeHSprinklersPoint[1]), hypiping6.GetAppropriateConnector(pipeHSprinklersPoint[1]));
						document.Create.NewElbowFitting(hypiping7.GetAppropriateConnector(pipeHSprinklersPoint2[1]), hypiping8.GetAppropriateConnector(pipeHSprinklersPoint2[1]));
						IList<Pipe> list4 = hypiping.Separate(pipeHSprinklersPoint[0]);
						document.Regenerate();
						if (list4 != null)
						{
							YJKPiping<Pipe> hypiping9 = new YJKPiping<Pipe>(list4[1]);
							document.Create.NewCrossFitting(hypiping.GetAppropriateConnector(pipeHSprinklersPoint[0]), hypiping9.GetAppropriateConnector(pipeHSprinklersPoint[0]), hypiping6.GetAppropriateConnector(pipeHSprinklersPoint[0]), hypiping8.GetAppropriateConnector(pipeHSprinklersPoint[0]));
							flag = true;
						}
						else
						{
							flag = false;
						}
					}
				}
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private Pipe CheckPipeEndPoint(YJKPiping<Pipe> hyPipe, YJKPiping<Pipe> hyPipe2, ref XYZ endPoint)
		{
			double num = hyPipe.StartConnector.Origin.DistanceTo(endPoint) + hyPipe.EndConnector.Origin.DistanceTo(endPoint);
			double num2 = hyPipe2.StartConnector.Origin.DistanceTo(endPoint) + hyPipe2.EndConnector.Origin.DistanceTo(endPoint);
			Pipe piping;
			if (num - num2 < 0.0001)
			{
				piping = hyPipe2.Piping;
				if (hyPipe2.StartConnector.Origin.DistanceTo(endPoint) > hyPipe2.EndConnector.Origin.DistanceTo(endPoint))
				{
					endPoint = hyPipe2.EndConnector.Origin;
				}
				else
				{
					endPoint = hyPipe2.StartConnector.Origin;
				}
			}
			else
			{
				piping = hyPipe.Piping;
				if (hyPipe.StartConnector.Origin.DistanceTo(endPoint) > hyPipe.EndConnector.Origin.DistanceTo(endPoint))
				{
					endPoint = hyPipe.EndConnector.Origin;
				}
				else
				{
					endPoint = hyPipe.StartConnector.Origin;
				}
			}
			return piping;
		}

		private Pipe CheckPipeEndPoint(YJKPiping<Pipe> hyPipe, ref XYZ endPoint)
		{
			Pipe piping = hyPipe.Piping;
			if (hyPipe.StartConnector.Origin.DistanceTo(endPoint) < hyPipe.EndConnector.Origin.DistanceTo(endPoint))
			{
				endPoint = hyPipe.StartConnector.Origin;
				return piping;
			}
			endPoint = hyPipe.EndConnector.Origin;
			return piping;
		}

		public IList<Pipe> PipeConnectHSprinklersCross(ref Pipe pipe, FamilyInstance hSprinklers, FamilyInstance hSprinklers2, ref XYZ endPoint)
		{
			IList<Pipe> list = new List<Pipe>();
			SubTransaction subTransaction = null;
			IList<Pipe> result;
			try
			{
				Document document = pipe.Document;
				Connector hsprinklersConnector = this.GetHSprinklersConnector(hSprinklers);
				Connector hsprinklersConnector2 = this.GetHSprinklersConnector(hSprinklers2);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<XYZ> list2 = new List<XYZ>();
				IList<XYZ> list3 = new List<XYZ>();
				list2.Add(hsprinklersConnector.Origin);
				list2.Add(this.GetConnectorDirectionPoint(hsprinklersConnector));
				list3.Add(hsprinklersConnector2.Origin);
				list3.Add(this.GetConnectorDirectionPoint(hsprinklersConnector2));
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				Curve curve = Line.CreateBound(list2[0], list2[1]);
				Curve curve2 = Line.CreateBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				Curve curve3 = Line.CreateBound(list3[0], list3[1]);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				curve3.MakeUnbound();
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				XYZ curveIntersection2 = this.GetCurveIntersection(curve3, curve2);
				if (curveIntersection != null && curveIntersection.IsAlmostEqualTo(curveIntersection2))
				{
					Pipe pipe2 = YJKRevitTools.NewPipe(pipe, curveIntersection, hsprinklersConnector.Origin);
					this.PipeSetParameter(hsprinklersConnector, pipe2);
					YJKRevitTools.ConnectTwoElement(hsprinklersConnector, pipe2);
					Pipe pipe3 = YJKRevitTools.NewPipe(pipe, curveIntersection, hsprinklersConnector2.Origin);
					this.PipeSetParameter(hsprinklersConnector2, pipe3);
					YJKRevitTools.ConnectTwoElement(hsprinklersConnector2, pipe3);
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
					YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipe3);
					IList<Pipe> list4 = hypiping.Separate(curveIntersection);
					document.Regenerate();
					if (list4 != null)
					{
						YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(list4[1]);
						document.Create.NewCrossFitting(hypiping.GetAppropriateConnector(curveIntersection), hypiping4.GetAppropriateConnector(curveIntersection), hypiping2.GetAppropriateConnector(curveIntersection), hypiping3.GetAppropriateConnector(curveIntersection));
						list.Add(hypiping2.Piping);
						list.Add(hypiping3.Piping);
						pipe = this.CheckPipeEndPoint(hypiping, hypiping4, ref endPoint);
					}
					else
					{
						list = null;
					}
				}
				else if (curveIntersection == null && curveIntersection2 == null)
				{
					int num = 1;
					int num2 = 1;
					IList<XYZ> pipeHSprinklersPoint = this.GetPipeHSprinklersPoint(list2[0], list2[1], hypiping, ref num);
					IList<XYZ> pipeHSprinklersPoint2 = this.GetPipeHSprinklersPoint(list3[0], list3[1], hypiping, ref num2);
					if (pipeHSprinklersPoint != null && pipeHSprinklersPoint.Count > 1 && pipeHSprinklersPoint2 != null && pipeHSprinklersPoint2.Count > 1 && pipeHSprinklersPoint[0].IsAlmostEqualTo(pipeHSprinklersPoint2[0]))
					{
						Pipe pipe4 = YJKRevitTools.NewPipe(pipe, pipeHSprinklersPoint[1], hsprinklersConnector.Origin);
						this.PipeSetParameter(hsprinklersConnector, pipe4);
						YJKRevitTools.ConnectTwoElement(hsprinklersConnector, pipe4);
						Pipe pipe5 = YJKRevitTools.NewPipe(hypiping.Piping, pipeHSprinklersPoint[0], pipeHSprinklersPoint[1]);
						this.PipeSetParameter(hsprinklersConnector, pipe5);
						Pipe pipe6 = YJKRevitTools.NewPipe(pipe, pipeHSprinklersPoint2[1], hsprinklersConnector2.Origin);
						this.PipeSetParameter(hsprinklersConnector2, pipe6);
						YJKRevitTools.ConnectTwoElement(hsprinklersConnector2, pipe6);
						Pipe pipe7 = YJKRevitTools.NewPipe(hypiping.Piping, pipeHSprinklersPoint2[0], pipeHSprinklersPoint2[1]);
						this.PipeSetParameter(hsprinklersConnector2, pipe7);
						document.Regenerate();
						YJKPiping<Pipe> hypiping5 = new YJKPiping<Pipe>(pipe4);
						YJKPiping<Pipe> hypiping6 = new YJKPiping<Pipe>(pipe5);
						YJKPiping<Pipe> hypiping7 = new YJKPiping<Pipe>(pipe6);
						YJKPiping<Pipe> hypiping8 = new YJKPiping<Pipe>(pipe7);
						document.Create.NewElbowFitting(hypiping5.GetAppropriateConnector(pipeHSprinklersPoint[1]), hypiping6.GetAppropriateConnector(pipeHSprinklersPoint[1]));
						document.Create.NewElbowFitting(hypiping7.GetAppropriateConnector(pipeHSprinklersPoint2[1]), hypiping8.GetAppropriateConnector(pipeHSprinklersPoint2[1]));
						if (num == 1 && num2 == 1)
						{
							IList<Pipe> list5 = hypiping.Separate(pipeHSprinklersPoint[0]);
							document.Regenerate();
							if (list5 != null)
							{
								YJKPiping<Pipe> hypiping9 = new YJKPiping<Pipe>(list5[1]);
								document.Create.NewCrossFitting(hypiping.GetAppropriateConnector(pipeHSprinklersPoint[0]), hypiping9.GetAppropriateConnector(pipeHSprinklersPoint[0]), hypiping6.GetAppropriateConnector(pipeHSprinklersPoint[0]), hypiping8.GetAppropriateConnector(pipeHSprinklersPoint[0]));
								list.Add(hypiping6.Piping);
								list.Add(hypiping8.Piping);
								pipe = this.CheckPipeEndPoint(hypiping, hypiping9, ref endPoint);
							}
							else
							{
								list = null;
							}
						}
						else
						{
							document.Create.NewTeeFitting(hypiping6.GetAppropriateConnector(pipeHSprinklersPoint[0]), hypiping8.GetAppropriateConnector(pipeHSprinklersPoint[0]), hypiping.GetAppropriateConnector(pipeHSprinklersPoint[0]));
							list.Add(hypiping6.Piping);
							list.Add(hypiping8.Piping);
							pipe = this.CheckPipeEndPoint(hypiping, ref endPoint);
						}
					}
				}
				subTransaction.Commit();
				result = list;
			}
			catch (Exception)
			{
				list = null;
				result = list;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public Pipe CheckPipeLength(Pipe pipe, IList<Element> listTermianl)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			List<XYZ> list = new List<XYZ>();
			List<XYZ> list2 = new List<XYZ>();
			List<XYZ> list3 = new List<XYZ>();
			foreach (Element element in listTermianl)
			{
				XYZ point;
				if (element is Pipe)
				{
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(element as Pipe);
					point = (hypiping2.StartConnector.Origin + hypiping2.EndConnector.Origin) / 2.0;
				}
				else
				{
					point = YJKRevitTools.GetLoctaionPoint(element);
				}
				list.Add(hypiping.GetProject(point));
				list2.Add(hypiping.GetLineProject(point));
			}
			bool flag = false;
			foreach (XYZ xyz in list2)
			{
				flag = false;
				using (List<XYZ>.Enumerator enumerator3 = list.GetEnumerator())
				{
					while (enumerator3.MoveNext())
					{
						if (enumerator3.Current.IsAlmostEqualTo(xyz))
						{
							flag = true;
						}
					}
				}
				if (!flag)
				{
					list3.Add(xyz);
				}
			}
			Pipe result;
			if (list3.Count > 0)
			{
				XYZ xyz2 = hypiping.StartConnector.Origin;
				XYZ xyz3 = hypiping.EndConnector.Origin;
				XYZ xyz4 = (xyz3 - xyz2).Normalize();
				foreach (XYZ xyz5 in list3)
				{
					if (xyz2.DistanceTo(xyz5) < xyz3.DistanceTo(xyz5))
					{
						if ((xyz2 - xyz5).Normalize().IsAlmostEqualTo(xyz4))
						{
							xyz2 = xyz5;
						}
					}
					else if ((xyz5 - xyz3).Normalize().IsAlmostEqualTo(xyz4))
					{
						xyz3 = xyz5;
					}
				}
				result = hypiping.Extend(xyz2, xyz3);
			}
			else
			{
				result = pipe;
			}
			return result;
		}

		public bool PipeConnectHSprinklersByRectangle(Pipe pipe, IList<Element> listSprinklers, int connectType)
		{
			if (pipe == null || listSprinklers == null)
			{
				return true;
			}
			this.pipeConnectType = connectType;
			bool flag = false;
			IList<AutoConnectParameter> list = null;
			bool result;
			try
			{
				Document document = pipe.Document;
				pipe = this.CheckPipeLength(pipe, listSprinklers);
				ConnectHelper connectHelper = new ConnectHelper();
				IList<AutoConnectParameter> listParam = connectHelper.InitYJKElement(pipe, listSprinklers);
				new List<Connector>();
				IList<Connector> pipeConnectorEnd = connectHelper.GetPipeConnectorEnd(pipe);
				XYZ xyz = pipeConnectorEnd[0].Origin;
				XYZ origin = pipeConnectorEnd[1].Origin;
				list = connectHelper.PositionAssign(connectHelper.OrderByDistanceOfEnd(xyz, origin, listParam), xyz, origin);
				IList<AutoConnectParameter> list2 = new List<AutoConnectParameter>();
				int num = 1;
				int i = 0;
				for (i = 0; i < list.Count; i++)
				{
					int j;
					for (j = i; j < list.Count; j++)
					{
						if (list[j].IndexOfDuct != num)
						{
							i = j - 1;
							break;
						}
						list2.Add(list[j]);
					}
					if (i != j - 1)
					{
						i = j - 1;
					}
					if (list2.Count > 0)
					{
						if (list2.Count > 1)
						{
							if (!connectHelper.IsElementAlign(list2))
							{
								List<Element> list3 = new List<Element>();
								foreach (AutoConnectParameter autoConnectParameter in list2)
								{
									list3.Add(document.GetElementById(autoConnectParameter.ElementId));
								}
								connectHelper.AlignElement(xyz, origin, list3);
							}
						}
						else if (list2[0].PositionFlag == 3 && list2[0].VectorToDuct.GetLength() < 0.33)
						{
							connectHelper.AlignElement(xyz, origin, document.GetElementById(list2[0].ElementId));
							list2[0].PositionFlag = 1;
						}
						else if (list2[0].PositionFlag == 1 && list2[0].VectorToDuct.GetLength() > 0.0001)
						{
							connectHelper.AlignElement(xyz, origin, document.GetElementById(list2[0].ElementId));
						}
						else if (list2[0].PositionFlag == 2)
						{
							connectHelper.AlignElement(xyz, origin, document.GetElementById(list2[0].ElementId));
						}
						if (list2[0].PositionFlag == 2)
						{
							if (!this.ConnectPipeSprinklersLoop2(document, ref pipe, list2))
							{
								return false;
							}
						}
						else if (list2[0].PositionFlag == 1)
						{
							xyz = this.ConnectPipeSprinklersLoop1(document, ref pipe, list2, xyz);
						}
						else if (list2[0].PositionFlag == 3)
						{
							IList<AutoConnectParameter> listParam2 = connectHelper.OrderByOfVector(list2);
							xyz = this.ConnectPipeSprinklersLoop3(document, ref pipe, listParam2, xyz);
						}
					}
					document.Regenerate();
					list2.Clear();
					num++;
					flag = true;
				}
				result = flag;
			}
			catch (Exception)
			{
				flag = false;
				result = flag;
			}
			return result;
		}

		public bool PipeConnectHSprinklersByRectangleType2(Pipe pipe, IList<Element> listSprinklers, double dSprinklerBranchOffset)
		{
			if (pipe == null || listSprinklers == null || listSprinklers.Count < 1)
			{
				return true;
			}
			bool flag = false;
			IList<AutoConnectParameter> list = null;
			bool result;
			try
			{
				string a = "";
				Document document = pipe.Document;
				pipe = this.CheckPipeLength(pipe, listSprinklers);
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				XYZ origin = hypiping.StartConnector.Connector.Origin;
				XYZ origin2 = hypiping.EndConnector.Connector.Origin;
				ConnectHelper connectHelper = new ConnectHelper();
				IList<AutoConnectParameter> listParam = connectHelper.InitYJKElement(pipe, listSprinklers);
				list = connectHelper.PositionAssign(connectHelper.OrderByDistanceOfEnd(origin, origin2, listParam), origin, origin2);
				Connector elementConnector = YJKRevitTools.GetElementConnector(listSprinklers[0]);
				LocationPoint locationPoint = listSprinklers[0].Location as LocationPoint;
				if (Math.Abs(elementConnector.Origin.Z - locationPoint.Point.Z) > dSprinklerBranchOffset * 1000.0 / 304.8)
				{
					result = false;
				}
				else
				{
					double num = elementConnector.Radius * 2.0;
					XYZ basisZ = elementConnector.CoordinateSystem.BasisZ;
					IList<AutoConnectParameter> list2 = new List<AutoConnectParameter>();
					int num2 = 1;
					int i = 0;
					int count = list.Count;
					FamilySymbol familySymbolByName = YJKRevitTools.GetFamilySymbolByName("Fitting", "水管三通", document, ref a);
					for (i = 0; i < count; i++)
					{
						int j;
						for (j = i; j < count; j++)
						{
							if (list[j].IndexOfDuct != num2)
							{
								i = j - 1;
								break;
							}
							list2.Add(list[j]);
						}
						if (i != j - 1)
						{
							i = j - 1;
						}
						if (list2.Count > 1)
						{
							if (!connectHelper.IsElementAlign(list2))
							{
								List<Element> list3 = new List<Element>();
								foreach (AutoConnectParameter autoConnectParameter in list2)
								{
									list3.Add(document.GetElementById(autoConnectParameter.ElementId));
								}
								connectHelper.AlignElement(origin, origin2, list3);
							}
							IList<Element> list4 = new List<Element>();
							double num3 = 0.0;
							XYZ xyz = null;
							XYZ xyz2 = null;
							for (int k = 0; k < list2.Count - 1; k++)
							{
								for (int l = k + 1; l < list2.Count; l++)
								{
									double num4 = YJKRevitTools.DistanceTwoXYZ(list2[k].LocationPoint.Point, list2[l].LocationPoint.Point);
									if (num4 > num3)
									{
										num3 = num4;
										xyz = list2[k].LocationPoint.Point + dSprinklerBranchOffset * 1000.0 / 304.8 * basisZ;
										xyz2 = list2[l].LocationPoint.Point + dSprinklerBranchOffset * 1000.0 / 304.8 * basisZ;
									}
								}
							}
							if (xyz != null)
							{
								YJKLine hyline = new YJKLine(xyz, xyz2);
								Pipe pipe2 = YJKRevitTools.NewPipe(pipe, xyz, xyz2);
								new YJKPiping<Pipe>(pipe2).SetDiameter(num * 304.8);
								document.Regenerate();
								foreach (AutoConnectParameter autoConnectParameter2 in list2)
								{
									list4.Add(document.GetElementById(autoConnectParameter2.ElementId));
								}
								list2.Clear();
								this.PipeConnectHSprinklersByRectangle(pipe2, list4, 1);
								bool flag2 = false;
								foreach (Element element in YJKElementFilter.GetElementByClass(typeof(Pipe), document, pipe.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId()))
								{
									YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(element as Pipe);
									if (hyline.IsPointOnLine(hypiping2.StartConnector.Origin) || hyline.IsPointOnLine(hypiping2.EndConnector.Origin))
									{
										XYZ dlineP = YJKRevitTools.GetDLineP(origin, origin2, hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
										if (hypiping2.IsPointOnPipingCurve(dlineP))
										{
											pipe = this.GetNextPipe(origin, hypiping2.Piping, pipe, document, familySymbolByName, ref a);
											flag2 = true;
											break;
										}
									}
								}
								if (!flag2)
								{
									Pipe pipe3 = null;
									Pipe item = null;
									Element nearElement = this.GetNearElement(list4, pipe);
									LocationPoint locationPoint2 = nearElement.Location as LocationPoint;
									Connector elementConnector2 = YJKRevitTools.GetElementConnector(nearElement);
									if (elementConnector2.IsConnected)
									{
										pipe3 = (YJKRevitTools.GetConnectConnector(elementConnector2).Owner as Pipe);
										YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipe3);
										Connector connector = (hypiping3.StartConnector.Connector.Origin.DistanceTo(elementConnector2.Origin) > hypiping3.EndConnector.Connector.Origin.DistanceTo(elementConnector2.Origin)) ? hypiping3.StartConnector.Connector : hypiping3.EndConnector.Connector;
										Connector connectConnector = YJKRevitTools.GetConnectConnector(connector);
										if ((int)((MechanicalFitting)(connectConnector.Owner as FamilyInstance).MEPModel).PartType == 5)
										{
											foreach (Connector connector2 in YJKRevitTools.GetElementAllConnector(connectConnector.Owner))
											{
												Connector connectConnector2 = YJKRevitTools.GetConnectConnector(connector2);
												if (connectConnector2.Owner.Id != connector.Owner.Id)
												{
													item = (connectConnector2.Owner as Pipe);
												}
											}
											document.DeleteElement(connectConnector.Owner);
											document.Regenerate();
										}
									}
									XYZ xyz3 = new XYZ(locationPoint2.Point.X, locationPoint2.Point.Y, xyz.Z);
									XYZ endXYZ = new XYZ(hypiping.GetProject(xyz3).X, hypiping.GetProject(xyz3).Y, xyz.Z);
									Pipe pipe4 = YJKRevitTools.NewPipe(pipe, xyz3, endXYZ);
									new YJKPiping<Pipe>(pipe4).SetDiameter(num * 304.8);
									document.Regenerate();
									IList<Pipe> list5 = new List<Pipe>();
									list5.Add(pipe3);
									list5.Add(item);
									list5.Add(pipe4);
									this.NewTeeFitting(ref list5, true);
									document.Regenerate();
									pipe = this.GetNextPipe(origin, pipe4, pipe, document, familySymbolByName, ref a);
									document.Regenerate();
								}
							}
						}
						else if (list2[0].PositionFlag == 3)
						{
							XYZ xyz4 = list2[0].LocationPoint.Point + dSprinklerBranchOffset * 1000.0 / 304.8 * basisZ;
							XYZ endXYZ2 = new XYZ(hypiping.GetProject(xyz4).X, hypiping.GetProject(xyz4).Y, xyz4.Z);
							Pipe pipe5 = YJKRevitTools.NewPipe(pipe, xyz4, endXYZ2);
							new YJKPiping<Pipe>(pipe5).SetDiameter(num * 304.8);
							document.Regenerate();
							if (!list2[0].Connector.IsConnected)
							{
								FamilyInstance sprinklers = document.GetElementById(list2[0].ElementId) as FamilyInstance;
								this.PipeConnectHSprinklersElbow(ref pipe5, sprinklers);
							}
							pipe = this.GetNextPipe(origin, pipe5, pipe, document, familySymbolByName, ref a);
						}
						else if (list2[0].PositionFlag == 1 && list2[0].VectorToDuct.GetLength() > 0.0001)
						{
							connectHelper.AlignElement(origin, origin2, document.GetElementById(list2[0].ElementId));
							this.ConnectPipeSprinklersLoop1(document, ref pipe, list2, origin);
						}
						else if (list2[0].PositionFlag == 2)
						{
							connectHelper.AlignElement(origin, origin2, document.GetElementById(list2[0].ElementId));
							this.ConnectPipeSprinklersLoop2(document, ref pipe, list2);
						}
						list2.Clear();
						document.Regenerate();
						num2++;
						if (a == "")
						{
							flag = true;
						}
					}
					result = flag;
				}
			}
			catch (Exception)
			{
				flag = false;
				result = flag;
			}
			return result;
		}

		private Element GetNearElement(IList<Element> branchSprinklers, Pipe pipe)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			List<double> list = new List<double>();
			for (int i = 0; i < branchSprinklers.Count; i++)
			{
				XYZ xyz;
				if (branchSprinklers[i] is Pipe)
				{
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(branchSprinklers[i] as Pipe);
					xyz = (hypiping2.StartConnector.Origin + hypiping2.EndConnector.Origin) / 2.0;
				}
				else
				{
					xyz = (branchSprinklers[i].Location as LocationPoint).Point;
				}
				double item = xyz.DistanceTo(hypiping.GetProject(xyz));
				list.Add(item);
			}
			double num = 99999.0;
			int index = 0;
			for (int j = 0; j < list.Count; j++)
			{
				if (num > list[j])
				{
					num = list[j];
					index = j;
				}
			}
			return branchSprinklers[index];
		}

		public Pipe GetNextPipe(XYZ startPoint, Pipe riserPipe, Pipe pipe, Autodesk.Revit.DB.Document doc, FamilySymbol fs, ref string message)
		{
			YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
			List<Pipe> list = new List<Pipe>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			bool flag = hypiping.IntersectionPoint(riserPipe) != null;
			XYZ xyz = hypiping.StartConnector.Origin.Subtract(hypiping.EndConnector.Origin).Normalize();
			if (!flag)
			{
				list = hypipeConnectorHelper.TPipeConnectRPipe(riserPipe, pipe, doc, fs, false, 4, ref message, false);
			}
			else
			{
				IList<Pipe> list2 = new List<Pipe>
				{
					riserPipe,
					pipe
				};
				this.NewSomeConnect(list2);
				foreach (Pipe pipe2 in list2)
				{
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
					XYZ xyz2 = hypiping2.StartConnector.Origin.Subtract(hypiping2.EndConnector.Origin).Normalize();
					if (xyz2.IsAlmostEqualTo(xyz) || xyz2.IsAlmostEqualTo(-xyz))
					{
						list.Add(pipe2);
					}
				}
			}
			if (list != null)
			{
				if (list.Count == 2)
				{
					YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(list.ElementAt(0));
					YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(list.ElementAt(1));
					pipe = ((hypiping3.GetDistanceSumEnd(startPoint) > hypiping4.GetDistanceSumEnd(startPoint)) ? list.ElementAt(0) : list.ElementAt(1));
				}
				list.Clear();
			}
			doc.Regenerate();
			return pipe;
		}

		private Pipe GetNextPipeing(XYZ startPoint, Pipe riserPipe, Pipe pipe, Autodesk.Revit.DB.Document doc, FamilySymbol fs, ref string message)
		{
			YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
			List<Pipe> list = new List<Pipe>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			bool flag = hypiping.IntersectionPoint(riserPipe) != null;
			XYZ xyz = hypiping.StartConnector.Origin.Subtract(hypiping.EndConnector.Origin).Normalize();
			if (!flag)
			{
				list = hypipeConnectorHelper.TPipeConnectRPipe(riserPipe, pipe, doc, fs, false, 4, ref message, false);
			}
			else
			{
				IList<Pipe> list2 = new List<Pipe>
				{
					pipe,
					riserPipe
				};
				this.NewSomeConnect(list2);
				foreach (Pipe pipe2 in list2)
				{
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
					XYZ xyz2 = hypiping2.StartConnector.Origin.Subtract(hypiping2.EndConnector.Origin).Normalize();
					if (xyz2.IsAlmostEqualTo(xyz) || xyz2.IsAlmostEqualTo(-xyz))
					{
						list.Add(pipe2);
					}
				}
			}
			if (list != null)
			{
				if (list.Count == 2)
				{
					YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(list.ElementAt(0));
					YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(list.ElementAt(1));
					pipe = ((hypiping3.GetDistanceSumEnd(startPoint) > hypiping4.GetDistanceSumEnd(startPoint)) ? list.ElementAt(0) : list.ElementAt(1));
				}
				list.Clear();
			}
			doc.Regenerate();
			return pipe;
		}

		private bool ConnectPipeSprinklersLoop2(Autodesk.Revit.DB.Document doc, ref Pipe pipe, IList<AutoConnectParameter> listParam)
		{
			bool result = false;
			foreach (AutoConnectParameter autoConnectParameter in listParam)
			{
				result = this.PipeConnectHSprinklersElbow(ref pipe, doc.GetElementById(autoConnectParameter.ElementId) as FamilyInstance);
			}
			return result;
		}

		private XYZ ConnectPipeSprinklersLoop1(Autodesk.Revit.DB.Document doc, ref Pipe pipe, IList<AutoConnectParameter> listParam, XYZ endPoint)
		{
			Pipe pipe2 = pipe;
			XYZ xyz = endPoint;
			foreach (AutoConnectParameter autoConnectParameter in listParam)
			{
				XYZ xyz2 = xyz;
				Pipe pipe3 = pipe2;
				if (this.pipeConnectType == 1)
				{
					this.PipeConnectHSprinklersTee(ref pipe3, doc.GetElementById(autoConnectParameter.ElementId) as FamilyInstance, ref xyz2);
				}
				else
				{
					this.PipeConnectHSprinklersTakeOff(pipe3, doc.GetElementById(autoConnectParameter.ElementId) as FamilyInstance);
				}
				xyz = xyz2;
				pipe2 = pipe3;
			}
			pipe = pipe2;
			return xyz;
		}

		private XYZ ConnectPipeSprinklersLoop3(Autodesk.Revit.DB.Document doc, ref Pipe pipe, IList<AutoConnectParameter> listParam, XYZ endPoint)
		{
			Pipe pipe2 = pipe;
			XYZ result = endPoint;
			int num = 0;
			for (int i = 0; i < listParam.Count; i++)
			{
				if (listParam[0].IndexOfElement == listParam[i].IndexOfElement)
				{
					num = i;
				}
			}
			if (num != 0)
			{
				IList<Pipe> list = this.PipeConnectHSprinklersCross(ref pipe2, doc.GetElementById(listParam[0].ElementId) as FamilyInstance, doc.GetElementById(listParam[num].ElementId) as FamilyInstance, ref result);
				ConnectHelper connectHelper = new ConnectHelper();
				IList<AutoConnectParameter> listParam2 = new List<AutoConnectParameter>();
				Pipe pipe3 = list[0];
				Pipe pipe4 = list[1];
				if (num > 1)
				{
					if (connectHelper.GetProjectOnPipe(list[0], doc.GetElementById(listParam[1].ElementId), 0.0001) != null)
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, 1);
						this.ConnectPipeSprinklersLoop1(doc, ref pipe3, listParam2, this.GetEndPointOfPipe(list[0], listParam[0].LocationPoint.Point));
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						this.ConnectPipeSprinklersLoop1(doc, ref pipe4, listParam2, this.GetEndPointOfPipe(list[1], listParam[num].LocationPoint.Point));
					}
					else
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, 1);
						this.ConnectPipeSprinklersLoop1(doc, ref pipe4, listParam2, this.GetEndPointOfPipe(list[1], listParam[0].LocationPoint.Point));
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						this.ConnectPipeSprinklersLoop1(doc, ref pipe3, listParam2, this.GetEndPointOfPipe(list[0], listParam[num].LocationPoint.Point));
					}
				}
				else if (num + 1 < listParam.Count)
				{
					if (connectHelper.GetProjectOnPipe(list[0], doc.GetElementById(listParam[num + 1].ElementId), 0.0001) != null)
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						this.ConnectPipeSprinklersLoop1(doc, ref pipe3, listParam2, this.GetEndPointOfPipe(list[0], listParam[num].LocationPoint.Point));
					}
					else
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						this.ConnectPipeSprinklersLoop1(doc, ref pipe4, listParam2, this.GetEndPointOfPipe(list[1], listParam[num].LocationPoint.Point));
					}
				}
			}
			else if (num == 0)
			{
				new ConnectHelper();
				Pipe pipe5 = null;
				if (this.pipeConnectType == 1)
				{
					pipe5 = this.PipeConnectHSprinklersTee(ref pipe2, doc.GetElementById(listParam[0].ElementId) as FamilyInstance, ref result);
				}
				else
				{
					pipe5 = this.PipeConnectHSprinklersTakeOff(pipe2, doc.GetElementById(listParam[0].ElementId) as FamilyInstance, true);
				}
				IList<AutoConnectParameter> listParam3 = new List<AutoConnectParameter>();
				listParam3 = this.GetElementOutFirst(doc, listParam, num + 1);
				this.ConnectPipeSprinklersLoop1(doc, ref pipe5, listParam3, this.GetEndPointOfPipe(pipe5, listParam[num].LocationPoint.Point));
			}
			pipe = pipe2;
			return result;
		}

		public XYZ GetEndPointOfPipe(Pipe pipe, XYZ point)
		{
			XYZ result = new XYZ();
			IList<Connector> pipeConnectorEnd = new ConnectHelper().GetPipeConnectorEnd(pipe);
			if (pipeConnectorEnd[0].Origin.DistanceTo(point) > pipeConnectorEnd[1].Origin.DistanceTo(point))
			{
				result = pipeConnectorEnd[1].Origin;
			}
			else
			{
				result = pipeConnectorEnd[0].Origin;
			}
			return result;
		}

		private IList<AutoConnectParameter> GetElementOutFirst(Autodesk.Revit.DB.Document doc, IList<AutoConnectParameter> listElem, int iPosition)
		{
			IList<AutoConnectParameter> list = new List<AutoConnectParameter>();
			for (int i = iPosition; i < listElem.Count; i++)
			{
				if (i < listElem.Count)
				{
					if (listElem[i].IndexOfElement <= 1)
					{
						break;
					}
					list.Add(listElem[i]);
				}
			}
			return list;
		}

		public bool NewBeatchPipeEquipment(IList<FamilyInstance> familyInstances, IList<Pipe> pipes)
		{
			bool result = false;
			IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
			List<YJKLine> list2 = new List<YJKLine>();
			List<XYZ> list3 = new List<XYZ>();
			foreach (Pipe piping in pipes)
			{
				YJKPiping<Pipe> item = new YJKPiping<Pipe>(piping);
				list.Add(item);
			}
			YJKPiping<Pipe> hypiping = null;
			new XYZ();
			XYZ xyz = new XYZ();
			foreach (YJKPiping<Pipe> hypiping2 in list)
			{
				hypiping = hypiping2;
				XYZ origin = hypiping2.StartConnector.Origin;
				xyz = hypiping2.EndConnector.Origin;
                //origin - xyz;
				IEnumerator<YJKPiping<Pipe>> enumerator3 = list.GetEnumerator();
				{
					while (enumerator3.MoveNext())
					{
						YJKPiping<Pipe> hypiping3 = enumerator3.Current;
						hypiping2.IsParallel(hypiping3.Piping);
						YJKLine item2 = new YJKLine(hypiping3.StartConnector.Origin, hypiping3.EndConnector.Origin);
						list2.Add(item2);
						if (!hypiping3.StartConnector.Connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(hypiping2.StartConnector.Connector.CoordinateSystem.BasisZ))
						{
							list3.Add(hypiping3.StartConnector.Origin);
						}
						else
						{
							list3.Add(hypiping3.EndConnector.Origin);
						}
					}
					break;
				}
			}
			IList<EquipmentCoilDouble> list4 = new List<EquipmentCoilDouble>();
			for (int i = 0; i < familyInstances.Count; i++)
			{
				if (familyInstances.Count == 1)
				{
					list4.Add(new EquipmentCoilDouble(familyInstances[i], null, false, this.PointToFiDistance(familyInstances[i], hypiping)));
				}
				else if (i >= familyInstances.Count - 1)
				{
					break;
				}
				bool flag = false;
				for (int j = i + 1; j < familyInstances.Count; j++)
				{
					if (this.IsAlignEquipment(familyInstances[i], familyInstances[j], hypiping))
					{
						list4.Add(new EquipmentCoilDouble(familyInstances[i], familyInstances[j], true, this.PointToFiDistance(familyInstances[i], hypiping)));
						flag = true;
						break;
					}
				}
				if (!flag && !this.IsExistInEquipmentDouble(list4, familyInstances[i]))
				{
					list4.Add(new EquipmentCoilDouble(familyInstances[i], null, false, this.PointToFiDistance(familyInstances[i], hypiping)));
				}
				if (!flag && i == familyInstances.Count - 2 && !this.IsExistInEquipmentDouble(list4, familyInstances[i + 1]))
				{
					list4.Add(new EquipmentCoilDouble(familyInstances[i + 1], null, false, this.PointToFiDistance(familyInstances[i + 1], hypiping)));
				}
			}
			list4 = (from c in list4
			orderby c.Position
			select c).ToList<EquipmentCoilDouble>();
			foreach (EquipmentCoilDouble equipmentCoilDouble in list4)
			{
				if (equipmentCoilDouble.IsDouble)
				{
					list.Clear();
					foreach (Pipe piping2 in pipes)
					{
						list.Add(new YJKPiping<Pipe>(piping2));
					}
					IList<YJKPiping<Pipe>> orderPipes = this.EquipmentConnect(equipmentCoilDouble, list, null);
					this.PipesConnect(orderPipes, (equipmentCoilDouble.ElemFirst as Element).Document, ref pipes, false, 0.0, null);
				}
				else
				{
					this.NewBeatchPipeEquipment(equipmentCoilDouble.ElemFirst as FamilyInstance, ref pipes, 1);
				}
				IList<Pipe> list5 = new List<Pipe>();
				foreach (YJKLine hyline in list2)
				{
					foreach (Pipe pipe in pipes)
					{
						bool flag2 = false;
						YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipe);
						if (hyline.IsLineOnLine(hypiping4.GetLocationCurve() as Line))
						{
							foreach (XYZ xyz2 in list3)
							{
								if (xyz2.IsAlmostEqualTo(hypiping4.StartConnector.Origin) || xyz2.IsAlmostEqualTo(hypiping4.EndConnector.Origin))
								{
									list5.Add(pipe);
									flag2 = true;
									break;
								}
							}
							if (flag2)
							{
								break;
							}
						}
					}
				}
				if (list5.Count == list2.Count)
				{
					pipes = list5;
				}
				else if (list5.Count > 0)
				{
					int count = list5.Count;
					int count2 = list2.Count;
				}
				result = true;
			}
			return result;
		}

		public bool NewBeatchPipeEquipment(IList<FamilyInstance> familyInstances, IList<Pipe> pipes, int type)
		{
			bool flag = false;
			bool result;
			try
			{
				IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
				List<YJKLine> list2 = new List<YJKLine>();
				List<XYZ> list3 = new List<XYZ>();
				foreach (Pipe piping in pipes)
				{
					YJKPiping<Pipe> item = new YJKPiping<Pipe>(piping);
					list.Add(item);
				}
				YJKPiping<Pipe> hypiping = null;
				new XYZ();
				XYZ xyz = new XYZ();
				foreach (YJKPiping<Pipe> hypiping2 in list)
				{
					hypiping = hypiping2;
					XYZ origin = hypiping2.StartConnector.Origin;
					xyz = hypiping2.EndConnector.Origin;
                    //origin - xyz;
					IEnumerator<YJKPiping<Pipe>> enumerator3 = list.GetEnumerator();
					{
						while (enumerator3.MoveNext())
						{
							YJKPiping<Pipe> hypiping3 = enumerator3.Current;
							hypiping2.IsParallel(hypiping3.Piping);
							YJKLine item2 = new YJKLine(hypiping3.StartConnector.Origin, hypiping3.EndConnector.Origin);
							list2.Add(item2);
							if (!hypiping3.StartConnector.Connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(hypiping2.StartConnector.Connector.CoordinateSystem.BasisZ))
							{
								list3.Add(hypiping3.StartConnector.Origin);
							}
							else
							{
								list3.Add(hypiping3.EndConnector.Origin);
							}
						}
						break;
					}
				}
				IList<EquipmentCoilDouble> list4 = new List<EquipmentCoilDouble>();
				for (int i = 0; i < familyInstances.Count; i++)
				{
					if (familyInstances.Count == 1)
					{
						list4.Add(new EquipmentCoilDouble(familyInstances[i], null, false, this.PointToFiDistance(familyInstances[i], hypiping)));
					}
					else if (i >= familyInstances.Count - 1)
					{
						break;
					}
					bool flag2 = false;
					for (int j = i + 1; j < familyInstances.Count; j++)
					{
						if (this.IsAlignEquipment(familyInstances[i], familyInstances[j], hypiping))
						{
							list4.Add(new EquipmentCoilDouble(familyInstances[i], familyInstances[j], true, this.PointToFiDistance(familyInstances[i], hypiping)));
							flag2 = true;
							break;
						}
					}
					if (!flag2 && !this.IsExistInEquipmentDouble(list4, familyInstances[i]))
					{
						list4.Add(new EquipmentCoilDouble(familyInstances[i], null, false, this.PointToFiDistance(familyInstances[i], hypiping)));
					}
					if (!flag2 && i == familyInstances.Count - 2 && !this.IsExistInEquipmentDouble(list4, familyInstances[i + 1]))
					{
						list4.Add(new EquipmentCoilDouble(familyInstances[i + 1], null, false, this.PointToFiDistance(familyInstances[i + 1], hypiping)));
					}
				}
				list4 = (from c in list4
				orderby c.Position
				select c).ToList<EquipmentCoilDouble>();
				foreach (EquipmentCoilDouble equipmentCoilDouble in list4)
				{
					if (equipmentCoilDouble.IsDouble)
					{
						list.Clear();
						foreach (Pipe piping2 in pipes)
						{
							list.Add(new YJKPiping<Pipe>(piping2));
						}
						IList<YJKPiping<Pipe>> orderPipes = this.EquipmentConnect(equipmentCoilDouble, list, null);
						this.PipesConnect(orderPipes, (equipmentCoilDouble.ElemFirst as Element).Document, ref pipes, false, 0.0, null);
					}
					else
					{
						this.NewBeatchPipeEquipment(equipmentCoilDouble.ElemFirst as FamilyInstance, ref pipes, type);
					}
					IList<Pipe> list5 = new List<Pipe>();
					foreach (YJKLine hyline in list2)
					{
						foreach (Pipe pipe in pipes)
						{
							bool flag3 = false;
							YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipe);
							if (hyline.IsLineOnLine(hypiping4.GetLocationCurve() as Line))
							{
								foreach (XYZ xyz2 in list3)
								{
									if (xyz2.IsAlmostEqualTo(hypiping4.StartConnector.Origin) || xyz2.IsAlmostEqualTo(hypiping4.EndConnector.Origin))
									{
										list5.Add(pipe);
										flag3 = true;
										break;
									}
								}
								if (flag3)
								{
									break;
								}
							}
						}
					}
					if (list5.Count == list2.Count)
					{
						pipes = list5;
					}
					else if (list5.Count > 0)
					{
						int count = list5.Count;
						int count2 = list2.Count;
					}
					flag = true;
				}
				result = flag;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public bool NewBeatchPipeEquipmentS(FamilyInstance familyInstance, Pipe pipe)
		{
			bool result = false;
			if (familyInstance != null)
			{
				double elevation = familyInstance.GetYJKLevel().Elevation;
				LayTrapsParam layTrapsParam = new LayTrapsParam();
				if (Math.Abs(elevation) < NumbericTools.mmToFeet(50.0))
				{
					layTrapsParam.TrapsType = 2;
				}
				else
				{
					layTrapsParam.TrapsType = 1;
				}
				layTrapsParam.Conn = YJKRevitTools.GetElementConnector(familyInstance);
				layTrapsParam.Fs = null;
				double num = layTrapsParam.Conn.Origin.Z - Math.Abs(elevation - NumbericTools.mmToFeet(150.0));
				layTrapsParam.SPipeXYZ = layTrapsParam.Conn.Origin + new XYZ(0.0, 0.0, -1.0) * num;
			}
			return result;
		}

		private bool IsExistInEquipmentDouble(IList<EquipmentCoilDouble> fiDoubleList, FamilyInstance fi)
		{
			bool result = false;
			foreach (EquipmentCoilDouble equipmentCoilDouble in fiDoubleList)
			{
				if (equipmentCoilDouble.ElemFirst != null && fi.Id == (equipmentCoilDouble.ElemFirst as FamilyInstance).Id)
				{
					result = true;
				}
				else if (equipmentCoilDouble.ElemSecond != null && fi.Id == (equipmentCoilDouble.ElemSecond as FamilyInstance).Id)
				{
					result = true;
				}
			}
			return result;
		}

		private IList<YJKPiping<Pipe>> EquipmentConnect(EquipmentCoilDouble ecd, IList<YJKPiping<Pipe>> hyPipes, List<FamilySymbol> fsList = null)
		{
			IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
			Document document = (ecd.ElemFirst as FamilyInstance).Document;
			List<Connector> elementConnector = YJKRevitTools.GetElementConnector(ecd.ElemFirst as FamilyInstance, (Autodesk.Revit.DB.Domain)3);
			List<Connector> elementConnector2 = YJKRevitTools.GetElementConnector(ecd.ElemSecond as FamilyInstance, (Autodesk.Revit.DB.Domain)3);
			foreach (Connector connector in elementConnector)
			{
				foreach (Connector connector2 in elementConnector2)
				{
					if (connector.PipeSystemType == connector2.PipeSystemType)
					{
						YJKPiping<Pipe> pipeTypeByConnector = this.GetPipeTypeByConnector(hyPipes, connector2);
						if (pipeTypeByConnector != null)
						{
							Pipe pipe = YJKRevitTools.NewPipe(pipeTypeByConnector.Piping, connector, connector2);
							document.Regenerate();
							elementConnector2.Remove(connector2);
							this.ConnectDoubleEquipment(ecd, pipe);
							if (fsList != null)
							{
								List<FamilySymbol> list2 = new List<FamilySymbol>();
								if ((int)connector.PipeSystemType == 7)
								{
									if (fsList[0] != null)
									{
										list2.Add(fsList[0]);
									}
									if (fsList[1] != null)
									{
										list2.Add(fsList[1]);
									}
								}
								else if ((int)connector.PipeSystemType == 8)
								{
									if (fsList[2] != null)
									{
										list2.Add(fsList[2]);
									}
									if (fsList[3] != null)
									{
										list2.Add(fsList[3]);
									}
								}
								if (list2.Count > 0)
								{
									GroupValueManage groupValueManage = new GroupValueManage(document, pipe, connector.Origin, 100.0, list2);
									if (!groupValueManage.Go())
									{
										throw new Exception("阀件插入失败，可能是阀件接口管设置太短");
									}
									pipe = groupValueManage.GetEndPipe();
									GroupValueManage groupValueManage2 = new GroupValueManage(document, pipe, connector2.Origin, 100.0, list2);
									if (!groupValueManage2.Go())
									{
										throw new Exception("阀件插入失败，可能是阀件接口管设置太短");
									}
									pipe = groupValueManage2.GetEndPipe();
								}
							}
							list.Add(pipeTypeByConnector);
							list.Add(new YJKPiping<Pipe>(pipe));
							document.Regenerate();
							break;
						}
					}
				}
			}
			document.Regenerate();
			return list;
		}

		private void ConnectDoubleEquipment(EquipmentCoilDouble ecd, Pipe pipe)
		{
			YJKRevitTools hyrevitTools = new YJKRevitTools();
			hyrevitTools.ConnectPipeToEquipment(ecd.ElemFirst as FamilyInstance, pipe);
			hyrevitTools.ConnectPipeToEquipment(ecd.ElemSecond as FamilyInstance, pipe);
		}

		private YJKPiping<Pipe> GetPipeTypeByConnector(IList<YJKPiping<Pipe>> hyPipes, Connector connector)
		{
			YJKPiping<Pipe> result = null;
			foreach (YJKPiping<Pipe> hypiping in hyPipes)
			{
				if (hypiping.StartConnector.Connector.PipeSystemType == connector.PipeSystemType)
				{
					result = hypiping;
					break;
				}
			}
			return result;
		}

		private double PointToFiDistance(FamilyInstance fi, YJKPiping<Pipe> hyPipe)
		{
			XYZ project = hyPipe.GetProject((fi.Location as LocationPoint).Point, true);
			return YJKRevitTools.DistanceTwoXYZ(hyPipe.StartConnector.Origin, project);
		}

		private bool IsAlignEquipment(FamilyInstance fi1, FamilyInstance fi2, YJKPiping<Pipe> hyVectorPipe)
		{
			bool result = false;
			XYZ xyz = hyVectorPipe.StartConnector.Origin - hyVectorPipe.EndConnector.Origin;
			XYZ point = (fi1.Location as LocationPoint).Point;
			XYZ point2 = (fi2.Location as LocationPoint).Point;
			XYZ start = new XYZ(point.X, point.Y, hyVectorPipe.StartConnector.Origin.Z);
			XYZ end = new XYZ(point2.X, point2.Y, hyVectorPipe.StartConnector.Origin.Z);
			XYZ xyz2 = point - point2;
			List<Connector> elementConnector = YJKRevitTools.GetElementConnector(fi1, (Autodesk.Revit.DB.Domain)3);
			List<Connector> elementConnector2 = YJKRevitTools.GetElementConnector(fi2, (Autodesk.Revit.DB.Domain)3);
			int num = 0;
			foreach (Connector connector in elementConnector)
			{
				foreach (Connector connector2 in elementConnector2)
				{
					if (connector.PipeSystemType == connector2.PipeSystemType && connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-xyz2.Normalize()) && connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(xyz2.Normalize()))
					{
						num++;
					}
				}
			}
			if (num == elementConnector.Count && Math.Abs(xyz2.AngleTo(xyz) - Math.PI*.5) < 0.0001)
			{
				YJKLine hyline = new YJKLine(start, end);
				YJKLine hyline2 = new YJKLine(hyVectorPipe.StartConnector.Origin, hyVectorPipe.EndConnector.Origin);
				if (hyline.IntersectionPoint(hyline2.Line) != null)
				{
					result = true;
				}
			}
			return result;
		}

		public bool NewBeatchPipeEquipment(FamilyInstance familyInstance, ref IList<Pipe> pipes, int equipmentType = 0)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (pipes == null || pipes.Count < 1 || familyInstance == null)
				{
					throw new System.ArgumentNullException("Pipe or Equipment", "is null");
				}
				Document document = pipes[0].Document;
				IList<Connector> hyelementConnector = new ConnectHelper().GetYJKElementConnector(familyInstance, (Autodesk.Revit.DB.Domain)3);
				IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				list = this.PipesAndConnectorOrderByType(hyelementConnector, pipes, document, equipmentType);
				if (list == null)
				{
					result = false;
				}
				else
				{
					bool flag = this.PipesConnect(list, document, ref pipes, false, 0.0, null);
					subTransaction.Commit();
					result = flag;
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null && 3 != (int)subTransaction.GetStatus())
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool NewBeatchPipeEquipment(FamilyInstance familyInstance, ref IList<Pipe> pipes, bool bFlex, double flexLength, int equipmentType = 0)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (pipes == null || pipes.Count < 1 || familyInstance == null)
				{
					throw new System.ArgumentNullException("Pipe or Equipment", "is null");
				}
				Document document = pipes[0].Document;
				IList<Connector> list = new ConnectHelper().GetYJKElementConnector(familyInstance, (Autodesk.Revit.DB.Domain)3);
				list = this.RemoveConnedConnectors(list);
				IList<YJKPiping<Pipe>> list2 = new List<YJKPiping<Pipe>>();
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				list2 = this.PipesAndConnectorOrderByType(list, pipes, document, bFlex, flexLength, equipmentType);
				if (list2 == null)
				{
					result = false;
				}
				else
				{
					bool flag = this.PipesConnect(list2, document, ref pipes, false, 0.0, null);
					subTransaction.Commit();
					result = flag;
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null && 3 != (int)subTransaction.GetStatus())
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private XYZ GetLocationPointToFamilyInstance(FamilyInstance fInstance)
		{
			XYZ result = null;
			LocationPoint locationPoint = fInstance.Location as LocationPoint;
			if (locationPoint != null)
			{
				result = locationPoint.Point;
			}
			return result;
		}

		private List<Connector> RemoveConnedConnectors(IList<Connector> conns)
		{
			List<Connector> list = new List<Connector>();
			foreach (Connector connector in conns)
			{
				if (connector != null && !connector.IsConnected)
				{
					list.Add(connector);
				}
			}
			return list;
		}

		public bool BeathchConnPipeEquipment(ref IList<Pipe> pipes, IList<Element> fInstances, int terminalConnType, XYZ globalPnt, List<FamilySymbol> fsList, bool bFlex = false, double flexLength = 0.0)
		{
			bool result = true;
			if (pipes == null || fInstances == null)
			{
				throw new Exception("水管或设备不能为空！");
			}
			try
			{
				string value = "";
				Document document = fInstances[0].Document;
				IEnumerator<Pipe> enumerator = pipes.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						Pipe pipe;
						if ((pipe = enumerator.Current) != null)
						{
							IList<FanCoilParameter> list = null;
							ConnectHelper connectHelper = new ConnectHelper();
							PipeSystemType connPipeSystemType = this.GetConnPipeSystemType(pipe, fInstances[0] as FamilyInstance);
							if (connPipeSystemType != null)
							{
								double distanceSystemTypeSet = this.GetDistanceSystemTypeSet(connPipeSystemType);
								IList<Connector> list2 = new List<Connector>();
								list2 = connectHelper.GetPipeConnectorEnd(pipe);
								IList<FanCoilParameter> list3 = connectHelper.InitYJKElement(pipe, distanceSystemTypeSet, fInstances);
								if (list3 == null || list3.Count < 1)
								{
									return false;
								}
								Connector connector;
								if (globalPnt == null)
								{
									connector = list2[0];
								}
								else
								{
									connector = ((list2[0].Origin.DistanceTo(globalPnt) > list2[1].Origin.DistanceTo(globalPnt)) ? list2[1] : list2[0]);
								}
								XYZ origin = list2[0].Origin;
								XYZ origin2 = list2[1].Origin;
								if (connector.Origin.DistanceTo(list2[0].Origin) > connector.Origin.DistanceTo(list2[1].Origin))
								{
									XYZ origin3 = list2[0].Origin;
									XYZ origin4 = list2[1].Origin;
								}
								else
								{
									XYZ origin5 = list2[1].Origin;
									XYZ origin6 = list2[0].Origin;
								}
								XYZ origin7 = connector.Origin;
								XYZ basisZ = connector.CoordinateSystem.BasisZ;
								list = connectHelper.OrderByDistanceOfEnd(origin7, list3);
								List<FanCoilParameter> list4 = new List<FanCoilParameter>();
								int num = 1;
								int i = 0;
								for (i = 0; i < list.Count; i++)
								{
									int j;
									for (j = i; j < list.Count; j++)
									{
										if (list[j].IndexOfPipe != num)
										{
											i = j - 1;
											break;
										}
										list4.Add(list[j]);
									}
									if (i != j - 1)
									{
										i = j - 1;
									}
									if (list4.Count > 0)
									{
										if (list4.Count == 1)
										{
											this.ConnectFanCoilAndPipe(ref pipe, list4[0], globalPnt, ref value, fsList, bFlex, flexLength);
										}
										else if (list4.Count > 1)
										{
											IList<List<FanCoilParameter>> list5 = connectHelper.OrderByFanCoilOfVector(list4);
											if (list5.Count > 1 && list5[1].Count > 0)
											{
												foreach (FanCoilParameter fanCoilParameter in list5[1])
												{
													Element element = null;
													foreach (Element element2 in fInstances)
													{
														if (element2.Id.IntegerValue.Equals(fanCoilParameter.ElementId.IntegerValue))
														{
															element = element2;
															break;
														}
													}
													if (element != null)
													{
														fInstances.Remove(element);
													}
												}
											}
											this.ConnectFanCoilsAndPipe(ref pipe, list5, globalPnt, basisZ, distanceSystemTypeSet, ref value, fsList, bFlex, flexLength);
										}
									}
									document.Regenerate();
									list4.Clear();
									num++;
									result = true;
								}
							}
						}
					}
				}
				if (!string.IsNullOrEmpty(value))
				{
					YJKMessageBox.Show("由于管道与风盘位置关系，\n有部分没有连接成功。");
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = false;
				return result;
			}
			return result;
		}

		private void ConnectFanCoilAndPipe(ref Pipe pipe, FanCoilParameter fcParam, XYZ basePnt, ref string connMSG, List<FamilySymbol> fsList, bool bflex = false, double flexlength = 0.0)
		{
			if (pipe == null)
			{
				return;
			}
			List<FamilySymbol> list = new List<FamilySymbol>();
			if ((int)fcParam.Connector.PipeSystemType == 7)
			{
				if (fsList[0] != null)
				{
					list.Add(fsList[0]);
				}
				if (fsList[1] != null)
				{
					list.Add(fsList[1]);
				}
			}
			else if ((int)fcParam.Connector.PipeSystemType == 8)
			{
				if (fsList[2] != null)
				{
					list.Add(fsList[2]);
				}
				if (fsList[3] != null)
				{
					list.Add(fsList[3]);
				}
			}
			else
			{
				bflex = false;
			}
			Pipe pipe2 = this.CreatFanCoilOutPipe(pipe, fcParam, bflex, flexlength);
			Pipe pipe3 = pipe2;
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			hyrotateFamilyInstance.MovePiping(pipe3);
			bool isAcc = false;
			if (list.Count > 0)
			{
				GroupValueManage groupValueManage = new GroupValueManage(pipe.Document, pipe2, fcParam.ConnPoint, 100.0, list, false);
				if (!groupValueManage.Go())
				{
					isAcc = true;
				}
				else
				{
					pipe3 = groupValueManage.GetEndPipe();
				}
			}
			hyrotateFamilyInstance.MovePiping(pipe3);
			this.ConnectSideAndMainPipe(pipe3, ref pipe, basePnt, ref connMSG, 0.01, isAcc, list);
		}

		private void ConnectFanCoilsAndPipe(ref Pipe pipe, IList<List<FanCoilParameter>> fcParams, XYZ basePnt, XYZ sysVector, double outLength, ref string connMSG, List<FamilySymbol> fsList, bool bFlex = false, double flexLength = 0.0)
		{
			if (fcParams == null || fcParams.Count < 1 || pipe == null)
			{
				return;
			}
			bool flag = false;
			if (this.GetPipeSlope(pipe) > 0.0)
			{
				flag = true;
			}
			Document document = pipe.Document;
			Pipe pipe2 = pipe;
			bool flag2 = false;
			Pipe pipe3 = null;
			IList<FanCoilParameter> list = null;
			list = fcParams[0];
			if (list == null || list.Count < 1)
			{
				return;
			}
			if (list.Count == 1)
			{
				this.ConnectFanCoilAndPipe(ref pipe2, list[0], basePnt, ref connMSG, fsList, bFlex, flexLength);
			}
			else
			{
				flag2 = false;
				IEnumerator<FanCoilParameter> enumerator = list.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						if (enumerator.Current.ReDirection == 1)
						{
							flag2 = true;
							break;
						}
					}
				}
				FanCoilParameter fanCoilParameter = list[0];
				XYZ xyz;
				XYZ xyz2;
				if (flag2)
				{
					xyz = fanCoilParameter.PointOnFace.Add(outLength * sysVector);
					xyz2 = fanCoilParameter.ExtendPoint.Add(outLength * sysVector);
				}
				else
				{
					xyz = fanCoilParameter.PointOnFace;
					xyz2 = fanCoilParameter.ExtendPoint;
				}
				XYZ startXYZ = xyz;
				if (flag)
				{
					startXYZ = this.GetSlopePipeEndPoint(xyz2, xyz, 0.003);
				}
				pipe3 = YJKRevitTools.NewPipe(pipe2, startXYZ, xyz2);
				this.SetDiameter(pipe3, fanCoilParameter.Connector.Radius * 2.0);
				this.ConnectSideAndMainPipe(pipe3, ref pipe2, basePnt, ref connMSG, 0.003, false, null);
				for (int i = list.Count - 1; i >= 0; i--)
				{
					this.ConnectFanCoilAndPipe(ref pipe3, list[i], fanCoilParameter.PointOnFace, ref connMSG, fsList, bFlex, flexLength);
				}
			}
			pipe = pipe2;
		}

		private Pipe CreatSystemSidePipe(Pipe basePipe, IList<FanCoilParameter> fanCoilParam, XYZ sysVector, double outLength, ref string connMSG)
		{
			bool flag = false;
			if (this.GetPipeSlope(basePipe) > 0.0)
			{
				flag = true;
			}
			bool flag2 = false;
			if (fanCoilParam == null || fanCoilParam.Count < 1)
			{
				return null;
			}
			IEnumerator<FanCoilParameter> enumerator = fanCoilParam.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.ReDirection == 1)
					{
						flag2 = true;
						break;
					}
				}
			}
			FanCoilParameter fanCoilParameter = fanCoilParam[0];
			XYZ xyz;
			XYZ xyz2;
			if (flag2)
			{
				xyz = fanCoilParameter.PointOnFace.Add(outLength * sysVector);
				xyz2 = fanCoilParameter.ExtendPoint.Add(outLength * sysVector);
			}
			else
			{
				xyz = fanCoilParameter.PointOnFace;
				xyz2 = fanCoilParameter.ExtendPoint;
			}
			XYZ endXYZ = xyz;
			if (flag)
			{
				endXYZ = this.GetSlopePipeEndPoint(xyz2, xyz, 0.003);
			}
			Pipe pipe = YJKRevitTools.NewPipe(basePipe, xyz2, endXYZ);
			this.SetDiameter(pipe, fanCoilParameter.Connector.Radius * 2.0);
			return pipe;
		}

		private Pipe ConnectSideAndMainPipe(Pipe sidePipe1, Pipe sidePipe2, ref Pipe mainPipe, XYZ basePnt, ref string connMSG)
		{
			Pipe pipe = null;
			Document document = sidePipe1.Document;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(sidePipe1);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(sidePipe2);
			YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(mainPipe);
			double diameter = hypiping.GetDiameter();
			hypiping2.GetDiameter();
			double diameter2 = hypiping3.GetDiameter();
			if (this.GetPipeSlope(mainPipe) > 0.0)
			{
				connMSG = "带坡度水管，不支持四通连接。";
				return pipe;
			}
			if (!hypiping.IsPipingOnLine(hypiping2.Piping))
			{
				connMSG = "两根支管须在同一直线上。";
				return pipe;
			}
			YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			YJKLine hyline2 = new YJKLine(hypiping3.EndConnector.Origin, hypiping3.StartConnector.Origin);
			if (!hyline2.IsVertical(hyline.Line))
			{
				connMSG = "两支管必须垂直于主管。";
				return pipe;
			}
			hyline2.MakeUnBound();
			XYZ project = hyline2.GetProject(hyline.StartPoint);
			XYZ xyz = new XYZ(project.X, project.Y, hyline.StartPoint.Z);
			double num = project.DistanceTo(xyz);
			IList<Pipe> list = new List<Pipe>();
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			if (num < 0.016404199475065617)
			{
				if (hyline2.IsPointOnLine(project) && hyline2.StartPoint.DistanceTo(project) > diameter2 * 1.5 && hyline2.EndPoint.DistanceTo(project) > diameter2 * 1.5)
				{
					List<Pipe> list2 = hypiping3.Separate(project);
					hyrotateFamilyInstance.MovePiping(list2[0]);
					hyrotateFamilyInstance.MovePiping(list2[1]);
					document.Regenerate();
					pipe = this.GetFarPipeToPoint(list2, basePnt);
					list = new List<Pipe>();
					list.Add(list2[0]);
					list.Add(list2[1]);
					list.Add(hypiping.Piping);
					list.Add(hypiping2.Piping);
					this.NewCrossFitting(ref list, true);
				}
				else
				{
					list = new List<Pipe>();
					list.Add(hypiping.Piping);
					list.Add(hypiping2.Piping);
					list.Add(hypiping3.Piping);
					this.NewTeeFitting(ref list, true);
				}
			}
			else if (num > (diameter + diameter2) * 1.5)
			{
				if (hyline2.IsPointOnLine(project) && hyline2.StartPoint.DistanceTo(project) > diameter2 * 1.5 && hyline2.EndPoint.DistanceTo(project) > diameter2 * 1.5)
				{
					List<Pipe> list2 = hypiping3.Separate(project);
					hyrotateFamilyInstance.MovePiping(list2[0]);
					hyrotateFamilyInstance.MovePiping(list2[1]);
					document.Regenerate();
					pipe = this.GetFarPipeToPoint(list2, basePnt);
					Pipe pipe2 = YJKRevitTools.NewPipe(sidePipe1, xyz, project);
					this.SetDiameter(pipe2, diameter);
					hyrotateFamilyInstance.MovePiping(pipe2);
					list = new List<Pipe>();
					list.Add(list2[0]);
					list.Add(list2[1]);
					list.Add(pipe2);
					this.NewTeeFitting(ref list, true);
					list = new List<Pipe>();
					list.Add(hypiping.Piping);
					list.Add(hypiping2.Piping);
					list.Add(pipe2);
					this.NewTeeFitting(ref list, true);
				}
				else
				{
					Pipe pipe3 = YJKRevitTools.NewPipe(sidePipe1, xyz, project);
					this.SetDiameter(pipe3, diameter);
					hyrotateFamilyInstance.MovePiping(pipe3);
					list = new List<Pipe>();
					list.Add(pipe3);
					list.Add(hypiping3.Piping);
					this.NewElbowFitting(list, true);
					list = new List<Pipe>();
					list.Add(hypiping.Piping);
					list.Add(hypiping2.Piping);
					list.Add(pipe3);
					this.NewTeeFitting(ref list, true);
				}
			}
			else
			{
				pipe = mainPipe;
			}
			mainPipe = pipe;
			return pipe;
		}

		private void ConnectSideAndMainPipe(Pipe sidePipe, ref Pipe mainPipe, XYZ basePnt, ref string connMSG, double slopeValue, bool isAcc = false, List<FamilySymbol> Accs = null)
		{
			if (sidePipe == null || mainPipe == null)
			{
				connMSG = "支管或主管不能为空！";
				return;
			}
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(sidePipe);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(mainPipe);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			XYZ origin3 = hypiping2.StartConnector.Origin;
			XYZ origin4 = hypiping2.EndConnector.Origin;
			YJKLine hyline = new YJKLine(new XYZ(origin.X, origin.Y, 0.0), new XYZ(origin2.X, origin2.Y, 0.0));
			YJKLine hyline2 = new YJKLine(new XYZ(origin3.X, origin3.Y, 0.0), new XYZ(origin4.X, origin4.Y, 0.0));
			if (this.GetPipeSlope(mainPipe) == 0.0)
			{
				if (hyline2.IsParallel(hyline.Line))
				{
					mainPipe = this.ConnParallelPipes(hypiping, hypiping2, basePnt, ref connMSG, isAcc, Accs);
					return;
				}
				mainPipe = this.ConnInterSectionPipes(hypiping.Piping, hypiping2.Piping, basePnt, ref connMSG, isAcc, Accs);
				return;
			}
			else
			{
				if (hyline2.IsParallel(hyline.Line))
				{
					mainPipe = this.ConnParallelPipes(hypiping, hypiping2, basePnt, ref connMSG, slopeValue, isAcc, Accs);
					return;
				}
				mainPipe = this.ConnInterSectionPipes(hypiping.Piping, hypiping2.Piping, basePnt, ref connMSG, slopeValue, isAcc, Accs);
				return;
			}
		}

		private Pipe ConnParallelPipes(YJKPiping<Pipe> hySide, YJKPiping<Pipe> hyMain, XYZ basePnt, ref string connMSG, bool isAcc = false, List<FamilySymbol> fsAcc = null)
		{
			Pipe result = hyMain.Piping;
			Document document = hySide.Piping.Document;
			XYZ xyz = hySide.StartConnector.Connector.IsConnected ? hySide.EndConnector.Origin : hySide.StartConnector.Origin;
			double diameter = hySide.GetDiameter();
			double diameter2 = hyMain.GetDiameter();
			YJKLine hyline = new YJKLine(hyMain.StartConnector.Origin, hyMain.EndConnector.Origin);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(xyz);
			XYZ xyz2 = new XYZ(project.X, project.Y, xyz.Z);
			double num = project.DistanceTo(xyz2);
			IList<Pipe> list = new List<Pipe>();
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			if (num <= 0.016404199475065617)
			{
				Pipe pipe = YJKRevitTools.NewPipe(hySide.Piping, xyz, project);
				this.SetDiameter(pipe, diameter);
				list.Add(pipe);
				list.Add(hySide.Piping);
				this.NewElbowFitting(list, true);
				if (hyline.IsPointOnLine(project) && hyline.StartPoint.DistanceTo(project) >= diameter2 * 1.5 && hyline.EndPoint.DistanceTo(project) >= diameter2 * 1.5)
				{
					List<Pipe> list2 = hyMain.Separate(project);
					hyrotateFamilyInstance.MovePiping(list2[0]);
					hyrotateFamilyInstance.MovePiping(list2[1]);
					hyrotateFamilyInstance.MovePiping(pipe);
					document.Regenerate();
					result = this.GetFarPipeToPoint(list2, basePnt);
					list = new List<Pipe>();
					list.Add(list2[0]);
					list.Add(list2[1]);
					list.Add(pipe);
					this.NewTeeFitting(ref list, true);
				}
				else
				{
					list = new List<Pipe>();
					list.Add(hyMain.Piping);
					list.Add(pipe);
					this.NewElbowFitting(list, true);
				}
				if (isAcc && !new GroupValueManage(pipe.Document, pipe, xyz, 100.0, fsAcc, true).Go())
				{
					throw new Exception("阀件插入失败");
				}
			}
			else if (num >= (diameter + diameter2) * 1.5)
			{
				Pipe pipe2 = YJKRevitTools.NewPipe(hySide.Piping, xyz, xyz2);
				this.SetDiameter(pipe2, diameter);
				list.Add(pipe2);
				list.Add(hySide.Piping);
				this.NewElbowFitting(list, true);
				Pipe pipe3 = YJKRevitTools.NewPipe(hySide.Piping, xyz2, project);
				this.SetDiameter(pipe3, diameter);
				list = new List<Pipe>();
				list.Add(pipe2);
				list.Add(pipe3);
				hyrotateFamilyInstance.MovePiping(pipe3);
				this.NewElbowFitting(list, true);
				if (hyline.IsPointOnLine(project) && hyline.StartPoint.DistanceTo(project) >= diameter2 * 1.5 && hyline.EndPoint.DistanceTo(project) >= diameter2 * 1.5)
				{
					List<Pipe> list2 = hyMain.Separate(project);
					hyrotateFamilyInstance.MovePiping(list2[0]);
					hyrotateFamilyInstance.MovePiping(list2[1]);
					hyrotateFamilyInstance.MovePiping(pipe3);
					document.Regenerate();
					result = this.GetFarPipeToPoint(list2, basePnt);
					list = new List<Pipe>();
					list.Add(list2[0]);
					list.Add(list2[1]);
					list.Add(pipe3);
					this.NewTeeFitting(ref list, true);
				}
				else
				{
					list = new List<Pipe>();
					list.Add(hyMain.Piping);
					list.Add(pipe3);
					this.NewElbowFitting(list, true);
				}
				if (isAcc && !new GroupValueManage(pipe2.Document, pipe2, xyz, 100.0, fsAcc, true).Go())
				{
					throw new Exception("阀件插入失败");
				}
			}
			else
			{
				connMSG = "两管间距不合适，不能进行连接";
			}
			return result;
		}

		private Pipe ConnParallelPipes(YJKPiping<Pipe> hySide, YJKPiping<Pipe> hyMain, XYZ basePnt, ref string connMSG, double slopeValue, bool isAcc = false, List<FamilySymbol> fsAcc = null)
		{
			Pipe result = hyMain.Piping;
			Document document = hySide.Piping.Document;
			XYZ xyz = hySide.StartConnector.Connector.IsConnected ? hySide.EndConnector.Origin : hySide.StartConnector.Origin;
			double diameter = hySide.GetDiameter();
			double diameter2 = hyMain.GetDiameter();
			YJKLine hyline = new YJKLine(hyMain.StartConnector.Origin, hyMain.EndConnector.Origin);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(xyz);
			XYZ initialEndPnt = new XYZ(project.X, project.Y, xyz.Z);
			XYZ slopePipeEndPoint = this.GetSlopePipeEndPoint(xyz, initialEndPnt, slopeValue);
			double num = slopePipeEndPoint.Z - project.Z;
			IList<Pipe> list = new List<Pipe>();
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			if (Math.Abs(num) <= (diameter + diameter2) / 2.0)
			{
				Pipe pipe = YJKRevitTools.NewPipe(hySide.Piping, xyz, project);
				this.SetDiameter(pipe, diameter);
				list.Add(pipe);
				list.Add(hySide.Piping);
				this.NewElbowFitting(list, true);
				if (hyline.IsPointOnLine(project) && hyline.StartPoint.DistanceTo(project) >= diameter2 * 1.5 && hyline.EndPoint.DistanceTo(project) >= diameter2 * 1.5)
				{
					List<Pipe> list2 = hyMain.Separate(project);
					hyrotateFamilyInstance.MovePiping(list2[0]);
					hyrotateFamilyInstance.MovePiping(list2[1]);
					document.Regenerate();
					result = this.GetFarPipeToPoint(list2, basePnt);
					list = new List<Pipe>();
					list.Add(list2[0]);
					list.Add(list2[1]);
					list.Add(pipe);
					this.NewTeeFitting(ref list, true);
				}
				else
				{
					list = new List<Pipe>();
					list.Add(hyMain.Piping);
					list.Add(pipe);
					this.NewElbowFitting(list, true);
				}
				if (isAcc && !new GroupValueManage(pipe.Document, pipe, xyz, 100.0, fsAcc, true).Go())
				{
					throw new Exception("阀件插入失败");
				}
			}
			else
			{
				if (num < 0.0)
				{
					connMSG = "主管道不能在支管的上方，不符合设计规则。";
					return result;
				}
				if (num < (diameter + diameter2) * 1.5)
				{
					connMSG = "主管与支管的间距过小，无法进行连接。";
					return result;
				}
				Pipe pipe2 = YJKRevitTools.NewPipe(hySide.Piping, xyz, slopePipeEndPoint);
				this.SetDiameter(pipe2, diameter);
				list.Add(pipe2);
				list.Add(hySide.Piping);
				this.NewElbowFitting(list, true);
				Pipe pipe3 = YJKRevitTools.NewPipe(hySide.Piping, slopePipeEndPoint, project);
				this.SetDiameter(pipe3, diameter);
				list = new List<Pipe>();
				list.Add(pipe2);
				list.Add(pipe3);
				this.NewElbowFitting(list, true);
				if (hyline.IsPointOnLine(project) && hyline.StartPoint.DistanceTo(project) >= diameter2 * 1.5 && hyline.EndPoint.DistanceTo(project) >= diameter2 * 1.5)
				{
					List<Pipe> list2 = hyMain.Separate(project);
					hyrotateFamilyInstance.MovePiping(list2[0]);
					hyrotateFamilyInstance.MovePiping(list2[1]);
					document.Regenerate();
					result = this.GetFarPipeToPoint(list2, basePnt);
					list = new List<Pipe>();
					list.Add(list2[0]);
					list.Add(list2[1]);
					list.Add(pipe3);
					this.NewTeeFitting(ref list, true);
				}
				else
				{
					list = new List<Pipe>();
					list.Add(hyMain.Piping);
					list.Add(pipe3);
					this.NewElbowFitting(list, true);
				}
				if (isAcc && !new GroupValueManage(pipe2.Document, pipe2, xyz, 100.0, fsAcc, true).Go())
				{
					throw new Exception("阀件插入失败");
				}
			}
			return result;
		}

		private XYZ GetSlopePipeEndPoint(XYZ startPnt, XYZ initialEndPnt, double slopeValue)
		{
			double num = startPnt.DistanceTo(initialEndPnt) * slopeValue;
			return new XYZ(initialEndPnt.X, initialEndPnt.Y, startPnt.Z - num);
		}

		private Pipe ConnInterSectionPipes(Pipe side, Pipe main, XYZ basePnt, ref string connMSG, bool isAcc = false, List<FamilySymbol> fsAcc = null)
		{
			Pipe result = main;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(side);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(main);
			Document document = side.Document;
			double diameter = hypiping.GetDiameter();
			double diameter2 = hypiping2.GetDiameter();
			YJKLine hyline = new YJKLine(hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
			hyline.MakeUnBound();
			YJKLine hyline2 = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			XYZ closePipePoint = this.GetClosePipePoint(hypiping, hypiping2);
			XYZ project = hyline.GetProject(closePipePoint);
			XYZ xyz = new XYZ(project.X, project.Y, closePipePoint.Z);
			double num = project.DistanceTo(xyz);
			IList<Pipe> list = new List<Pipe>();
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			if (num <= 0.016404199475065617)
			{
				Pipe pipe = hypiping.Piping;
				if (!hyline2.IsPointOnCurveLine(project))
				{
					Pipe pipe2 = YJKRevitTools.NewPipe(hypiping.Piping, closePipePoint, project);
					this.SetDiameter(pipe, diameter);
					list = new List<Pipe>();
					list.Add(pipe);
					list.Add(pipe2);
					this.NewElbowFitting(list, true);
					pipe = pipe2;
				}
				if (hyline.IsPointOnLine(project) && hyline.StartPoint.DistanceTo(project) >= diameter2 * 1.5 && hyline.EndPoint.DistanceTo(project) >= diameter2 * 1.5)
				{
					List<Pipe> list2 = hypiping2.Separate(project);
					hyrotateFamilyInstance.MovePiping(list2[0]);
					hyrotateFamilyInstance.MovePiping(list2[1]);
					hyrotateFamilyInstance.MovePiping(pipe);
					document.Regenerate();
					result = this.GetFarPipeToPoint(list2, basePnt);
					list = new List<Pipe>();
					list.Add(list2[0]);
					list.Add(list2[1]);
					list.Add(pipe);
					this.NewTeeFitting(ref list, true);
				}
				else
				{
					list = new List<Pipe>();
					list.Add(hypiping2.Piping);
					list.Add(pipe);
					this.NewElbowFitting(list, true);
				}
				if (isAcc && !new GroupValueManage(pipe.Document, pipe, closePipePoint, 100.0, fsAcc, true).Go())
				{
					throw new Exception("阀件插入失败");
				}
			}
			else if (num > (diameter + diameter2) * 1.5)
			{
				Pipe pipe3 = hypiping.Piping;
				if (!hyline2.IsPointOnCurveLine(xyz))
				{
					Pipe pipe4 = YJKRevitTools.NewPipe(hypiping.Piping, closePipePoint, xyz);
					this.SetDiameter(pipe3, diameter);
					list = new List<Pipe>();
					list.Add(pipe3);
					list.Add(pipe4);
					this.NewElbowFitting(list, true);
					pipe3 = pipe4;
				}
				Pipe pipe5 = YJKRevitTools.NewPipe(hypiping.Piping, xyz, project);
				this.SetDiameter(pipe5, diameter);
				hyrotateFamilyInstance.MovePiping(pipe5);
				list = new List<Pipe>();
				list.Add(pipe3);
				list.Add(pipe5);
				this.NewElbowFitting(list, true);
				if (hyline.IsPointOnLine(project) && hyline.StartPoint.DistanceTo(project) >= diameter2 * 1.5 && hyline.EndPoint.DistanceTo(project) >= diameter2 * 1.5)
				{
					List<Pipe> list2 = hypiping2.Separate(project);
					hyrotateFamilyInstance.MovePiping(list2[0]);
					hyrotateFamilyInstance.MovePiping(list2[1]);
					hyrotateFamilyInstance.MovePiping(pipe5);
					document.Regenerate();
					result = this.GetFarPipeToPoint(list2, basePnt);
					list = new List<Pipe>();
					list.Add(list2[0]);
					list.Add(list2[1]);
					list.Add(pipe5);
					this.NewTeeFitting(ref list, true);
				}
				else
				{
					list = new List<Pipe>();
					list.Add(hypiping2.Piping);
					list.Add(pipe5);
					this.NewElbowFitting(list, true);
				}
				if (isAcc && !new GroupValueManage(pipe3.Document, pipe3, closePipePoint, 100.0, fsAcc, true).Go())
				{
					throw new Exception("阀件插入失败");
				}
			}
			else
			{
				connMSG = "两管间距不合适，不能进行连接";
			}
			return result;
		}

		private Pipe ConnInterSectionPipes(Pipe side, Pipe main, XYZ basePnt, ref string connMSG, double slopeValue, bool isAcc = false, List<FamilySymbol> fsAcc = null)
		{
			Pipe result = main;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(side);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(main);
			Document document = side.Document;
			double diameter = hypiping.GetDiameter();
			double diameter2 = hypiping2.GetDiameter();
			YJKLine hyline = new YJKLine(hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
			hyline.MakeUnBound();
			YJKLine hyline2 = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			XYZ closePipePoint = this.GetClosePipePoint(hypiping, hypiping2);
			XYZ project = hyline.GetProject(closePipePoint);
			XYZ xyz = new XYZ(project.X, project.Y, closePipePoint.Z);
			double num = this.GetSlopePipeEndPoint(closePipePoint, xyz, slopeValue).Z - project.Z;
			IList<Pipe> list = new List<Pipe>();
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			hyline2 = new YJKLine(new XYZ(origin.X, origin.Y, closePipePoint.Z), new XYZ(origin2.X, origin2.Y, closePipePoint.Z));
			if (Math.Abs(num) <= (diameter + diameter2) / 2.0)
			{
				Pipe pipe = hypiping.Piping;
				if (!hyline2.IsPointOnCurveLine(xyz))
				{
					Pipe pipe2 = YJKRevitTools.NewPipe(hypiping.Piping, closePipePoint, project);
					this.SetDiameter(pipe, diameter);
					list = new List<Pipe>();
					list.Add(pipe);
					list.Add(pipe2);
					this.NewElbowFitting(list, true);
					pipe = pipe2;
				}
				if (hyline.IsPointOnLine(project) && hyline.StartPoint.DistanceTo(project) >= diameter2 * 1.5 && hyline.EndPoint.DistanceTo(project) >= diameter2 * 1.5)
				{
					List<Pipe> list2 = hypiping2.Separate(project);
					hyrotateFamilyInstance.MovePiping(list2[0]);
					hyrotateFamilyInstance.MovePiping(list2[1]);
					document.Regenerate();
					result = this.GetFarPipeToPoint(list2, basePnt);
					list = new List<Pipe>();
					list.Add(list2[0]);
					list.Add(list2[1]);
					list.Add(pipe);
					this.NewTeeFitting(ref list, true);
				}
				else
				{
					list = new List<Pipe>();
					list.Add(hypiping2.Piping);
					list.Add(pipe);
					this.NewElbowFitting(list, true);
				}
				if (isAcc && !new GroupValueManage(pipe.Document, pipe, closePipePoint, 100.0, fsAcc, true).Go())
				{
					throw new Exception("阀件插入失败");
				}
			}
			else
			{
				if (num < 0.0)
				{
					connMSG = "主管道不能在支管的上方，不符合设计规则。";
					return result;
				}
				if (num >= (diameter + diameter2) * 1.5)
				{
					XYZ xyz2 = xyz;
					Pipe pipe3 = hypiping.Piping;
					if (!hyline2.IsPointOnCurveLine(xyz))
					{
						xyz2 = this.GetSlopePipeEndPoint(closePipePoint, xyz, slopeValue);
						Pipe pipe4 = YJKRevitTools.NewPipe(hypiping.Piping, closePipePoint, xyz2);
						this.SetDiameter(pipe3, diameter);
						list = new List<Pipe>();
						list.Add(pipe3);
						list.Add(pipe4);
						this.NewElbowFitting(list, true);
						pipe3 = pipe4;
					}
					Pipe pipe5 = YJKRevitTools.NewPipe(hypiping.Piping, xyz2, project);
					this.SetDiameter(pipe5, diameter);
					hyrotateFamilyInstance.MovePiping(pipe5);
					list = new List<Pipe>();
					list.Add(pipe3);
					list.Add(pipe5);
					this.NewElbowFitting(list, true);
					if (hyline.IsPointOnLine(project) && hyline.StartPoint.DistanceTo(project) >= diameter2 * 1.5 && hyline.EndPoint.DistanceTo(project) >= diameter2 * 1.5)
					{
						List<Pipe> list2 = hypiping2.Separate(project);
						hyrotateFamilyInstance.MovePiping(list2[0]);
						hyrotateFamilyInstance.MovePiping(list2[1]);
						document.Regenerate();
						result = this.GetFarPipeToPoint(list2, basePnt);
						list = new List<Pipe>();
						list.Add(list2[0]);
						list.Add(list2[1]);
						list.Add(pipe5);
						this.NewTeeFitting(ref list, true);
					}
					else
					{
						list = new List<Pipe>();
						list.Add(hypiping2.Piping);
						list.Add(pipe5);
						this.NewElbowFitting(list, true);
					}
					if (isAcc && !new GroupValueManage(pipe3.Document, pipe3, closePipePoint, 100.0, fsAcc, true).Go())
					{
						throw new Exception("阀件插入失败");
					}
				}
				else
				{
					connMSG = "两管间距不合适，不能进行连接";
				}
			}
			return result;
		}

		private XYZ GetClosePipePoint(YJKPiping<Pipe> hySide, YJKPiping<Pipe> hyMain)
		{
			YJKLine hyline = new YJKLine(hySide.StartConnector.Origin, hySide.EndConnector.Origin);
			YJKLine hyline2 = new YJKLine(hyMain.StartConnector.Origin, hyMain.EndConnector.Origin);
			hyline2.MakeUnBound();
			if (hyline2.GetDistance(hyline.EndPoint) <= hyline2.GetDistance(hyline.StartPoint))
			{
				return hyline.EndPoint;
			}
			return hyline.StartPoint;
		}

		private double GetPipeSlope(Pipe pipe)
		{
			Document document = pipe.Document;
			return pipe.get_Parameter(BuiltInParameter.RBS_PIPE_SLOPE).AsDouble();
		}

		private Pipe GetFarPipeToPoint(List<Pipe> listPipe, XYZ point)
		{
			Pipe result = null;
			double num = 0.0;
			foreach (Pipe pipe in listPipe)
			{
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				XYZ xyz = hypiping.StartConnector.Origin.Add(hypiping.EndConnector.Origin) / 2.0;
				if (xyz.DistanceTo(point) > num)
				{
					num = xyz.DistanceTo(point);
					result = pipe;
				}
			}
			return result;
		}

		private Pipe ConnectOutAndMainPipe(Pipe outPipe, Pipe mainPipe, XYZ basePnt)
		{
			IList<Pipe> list = new List<Pipe>();
			List<YJKPiping<Pipe>> list2 = new List<YJKPiping<Pipe>>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(mainPipe);
			XYZ xyz = hypiping.StartConnector.Origin.Subtract(hypiping.EndConnector.Origin).Normalize();
			list2.Add(new YJKPiping<Pipe>(outPipe));
			list2.Add(new YJKPiping<Pipe>(mainPipe));
			this.PipesConnect(list2, mainPipe.Document, ref list, false, 0.0, null);
			List<YJKPiping<Pipe>> list3 = new List<YJKPiping<Pipe>>();
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(outPipe);
			foreach (Pipe piping in list)
			{
				YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(piping);
				XYZ xyz2 = hypiping3.StartConnector.Origin.Subtract(hypiping3.EndConnector.Origin).Normalize();
				if ((xyz2.IsAlmostEqualTo(xyz, 0.0001) || xyz2.IsAlmostEqualTo(-1.0 * xyz, 0.0001)) && hypiping2.Piping.UniqueId != hypiping3.Piping.UniqueId)
				{
					list3.Add(hypiping3);
				}
			}
			Pipe result;
			if (list3.Count <= 1)
			{
				result = null;
			}
			else if (list3.Count == 2)
			{
				XYZ xyz3 = list3[0].StartConnector.Origin.Add(list3[0].EndConnector.Origin) / 2.0;
				XYZ xyz4 = list3[1].StartConnector.Origin.Add(list3[1].EndConnector.Origin) / 2.0;
				result = ((basePnt.DistanceTo(xyz3) > xyz4.DistanceTo(basePnt)) ? list3[0].Piping : list3[1].Piping);
			}
			else
			{
				result = null;
			}
			return result;
		}

		private Pipe ConnectOutAndMainPipe(Pipe outPipe1, Pipe outPipe2, Pipe mainPipe, XYZ basePnt)
		{
			IList<Pipe> list = new List<Pipe>();
			List<YJKPiping<Pipe>> list2 = new List<YJKPiping<Pipe>>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(mainPipe);
			XYZ xyz = hypiping.StartConnector.Origin.Subtract(hypiping.EndConnector.Origin).Normalize();
			list2.Add(new YJKPiping<Pipe>(outPipe1));
			list2.Add(new YJKPiping<Pipe>(outPipe2));
			list2.Add(new YJKPiping<Pipe>(mainPipe));
			this.PipesConnect(list2, mainPipe.Document, ref list, false, 0.0, null);
			List<YJKPiping<Pipe>> list3 = new List<YJKPiping<Pipe>>();
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(outPipe1);
			YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(outPipe2);
			foreach (Pipe piping in list)
			{
				YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(piping);
				XYZ xyz2 = hypiping4.StartConnector.Origin.Subtract(hypiping4.EndConnector.Origin).Normalize();
				if ((xyz2.IsAlmostEqualTo(xyz, 0.0001) || xyz2.IsAlmostEqualTo(-1.0 * xyz, 0.0001)) && hypiping2.Piping.UniqueId != hypiping4.Piping.UniqueId && hypiping3.Piping.UniqueId != hypiping4.Piping.UniqueId)
				{
					list3.Add(hypiping4);
				}
			}
			Pipe result;
			if (list3.Count <= 1)
			{
				result = null;
			}
			else if (list3.Count == 2)
			{
				XYZ xyz3 = list3[0].StartConnector.Origin.Add(list3[0].EndConnector.Origin) / 2.0;
				XYZ xyz4 = list3[1].StartConnector.Origin.Add(list3[1].EndConnector.Origin) / 2.0;
				result = ((basePnt.DistanceTo(xyz3) > xyz4.DistanceTo(basePnt)) ? list3[0].Piping : list3[1].Piping);
			}
			else
			{
				result = null;
			}
			return result;
		}

		private void ConnectPipeTerminalLoop2(Autodesk.Revit.DB.Document doc, Pipe pipe, IList<FanCoilParameter> listParam, ref string connMSG)
		{
			foreach (FanCoilParameter fcParam in listParam)
			{
				Pipe pipe2 = this.ConnectFanCoil(doc, pipe, fcParam);
				this.ElbowSlopePipeConnector(pipe2, pipe, ref connMSG);
				doc.Regenerate();
			}
		}

		private XYZ ConnectPipeTerminalLoop1(Autodesk.Revit.DB.Document doc, ref Pipe pipe, IList<FanCoilParameter> listParam, int connType, XYZ basePnt, ref string connMSG)
		{
			Pipe pipe2 = pipe;
			XYZ xyz = basePnt;
			foreach (FanCoilParameter fanCoilParameter in listParam)
			{
				Pipe pipe3 = this.ConnectFanCoil(doc, pipe, fanCoilParameter);
				double num = fanCoilParameter.PointOnPipe.DistanceTo(fanCoilParameter.PointOnFace);
				double num2 = fanCoilParameter.Connector.Radius * 2.0;
				double diameter = new YJKPiping<Pipe>(pipe).GetDiameter();
				if (num > (diameter + num2) * 1.5)
				{
					Pipe pipe4 = doc.CreatHYPipe(this.GetPipeSystemType(pipe), this.GetPipeType(pipe), doc.ActiveView.GenLevel, fanCoilParameter.PointOnFace, fanCoilParameter.PointOnPipe, num2);
					this.ElbowConnect(pipe3, pipe4, fanCoilParameter.PointOnFace, fanCoilParameter.PointOnFace);
					doc.Regenerate();
				}
				XYZ xyz2 = xyz;
				Pipe pipe5 = pipe2;
				xyz = xyz2;
				pipe2 = pipe5;
			}
			doc.Regenerate();
			pipe = pipe2;
			return xyz;
		}

		private XYZ ConnectPipeTerminalLoop3(Autodesk.Revit.DB.Document doc, ref Pipe pipe, IList<FanCoilParameter> listParam, int connType, XYZ basePnt, ref string connMSG)
		{
			Pipe pipe2 = pipe;
			int num = 0;
			for (int i = 0; i < listParam.Count; i++)
			{
				if (listParam[0].IndexOfElement == listParam[i].IndexOfElement)
				{
					num = i;
				}
			}
			IList<Pipe> list = null;
			if (num != 0)
			{
				Pipe pipe3 = this.ConnectFanCoil(doc, pipe, listParam[0]);
				Pipe pipe4 = this.ConnectFanCoil(doc, pipe, listParam[num]);
				XYZ basePnt2 = listParam[0].ConnPoint;
				XYZ basePnt3 = listParam[num].ConnPoint;
				double num2 = listParam[0].Connector.Radius * 2.0;
				double diameter = new YJKPiping<Pipe>(pipe).GetDiameter();
				if (listParam[0].PointOnFace.IsAlmostEqualTo(listParam[num].PointOnFace, 0.001))
				{
					double num3 = listParam[0].PointOnFace.DistanceTo(listParam[0].PointOnPipe);
					if (num3 <= 0.016404199475065617)
					{
						list.Add(pipe3);
						list.Add(pipe4);
						list.Add(pipe);
						list.Add(pipe);
					}
					else if (num3 > (num2 + diameter) * 1.5)
					{
						this.CreatPipe(pipe, listParam[0].PointOnFace, listParam[0].PointOnPipe, num2);
					}
				}
				ConnectHelper connectHelper = new ConnectHelper();
				IList<FanCoilParameter> listParam2 = new List<FanCoilParameter>();
				if (num > 1)
				{
					if (connectHelper.GetProjectOnPipe(pipe3, listParam[1].ExtendPoint) != null)
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, 1);
						basePnt2 = this.GetPipeEndPoint(pipe3, listParam[0].ExtendPoint);
						this.ConnectPipeTerminalLoop1(doc, ref pipe3, listParam2, connType, basePnt2, ref connMSG);
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						basePnt3 = this.GetPipeEndPoint(pipe4, listParam[num].ExtendPoint);
						this.ConnectPipeTerminalLoop1(doc, ref pipe4, listParam2, connType, basePnt3, ref connMSG);
					}
					else
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, 1);
						basePnt2 = this.GetPipeEndPoint(pipe4, listParam[0].ExtendPoint);
						this.ConnectPipeTerminalLoop1(doc, ref pipe4, listParam2, connType, basePnt2, ref connMSG);
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						basePnt3 = this.GetPipeEndPoint(pipe3, listParam[num].ExtendPoint);
						this.ConnectPipeTerminalLoop1(doc, ref pipe3, listParam2, connType, basePnt3, ref connMSG);
					}
				}
				else if (num + 1 < listParam.Count)
				{
					if (connectHelper.GetProjectOnPipe(pipe3, listParam[1].ExtendPoint) != null)
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						basePnt3 = this.GetPipeEndPoint(pipe3, listParam[num].ExtendPoint);
						this.ConnectPipeTerminalLoop1(doc, ref pipe3, listParam2, connType, basePnt3, ref connMSG);
					}
					else
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						basePnt3 = this.GetPipeEndPoint(pipe4, listParam[num].ExtendPoint);
						this.ConnectPipeTerminalLoop1(doc, ref pipe4, listParam2, connType, basePnt3, ref connMSG);
					}
				}
			}
			else if (num == 0)
			{
				Pipe pipe5 = this.ConnectFanCoil(doc, pipe, listParam[0]);
				IList<FanCoilParameter> listParam3 = new List<FanCoilParameter>();
				listParam3 = this.GetElementOutFirst(doc, listParam, num + 1);
				XYZ pipeEndPoint = this.GetPipeEndPoint(pipe5, listParam[num].ExtendPoint);
				this.ConnectPipeTerminalLoop1(doc, ref pipe5, listParam3, connType, pipeEndPoint, ref connMSG);
			}
			pipe = pipe2;
			return basePnt;
		}

		private XYZ GetEndPointOfDuct(Pipe pipe, XYZ point)
		{
			XYZ result = new XYZ();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			IList<Connector> list = new List<Connector>();
			list.Add(hypiping.StartConnector.Connector);
			list.Add(hypiping.EndConnector.Connector);
			if (list[0].Origin.DistanceTo(point) > list[1].Origin.DistanceTo(point))
			{
				result = list[1].Origin;
			}
			else
			{
				result = list[0].Origin;
			}
			return result;
		}

		private Pipe ConnectFanCoil(Autodesk.Revit.DB.Document doc, Pipe pipe, FanCoilParameter fcParam)
		{
			double diameter = fcParam.Connector.Radius * 2.0;
			Pipe pipe2 = YJKRevitTools.NewPipe(pipe, fcParam.ConnPoint, fcParam.ExtendPoint);
			this.SetDiameter(pipe2, diameter);
			this.ConnPipeAndConnector(pipe2, fcParam.Connector, false, 0.0);
			Pipe pipe3;
			if (fcParam.ExtendPoint.IsAlmostEqualTo(fcParam.PointOnFace, 0.0001))
			{
				pipe3 = pipe2;
			}
			else if (fcParam.TurnPoint == null)
			{
				YJKLine hyline = new YJKLine(fcParam.ConnPoint, fcParam.ExtendPoint);
				if (hyline.IsPointOnLine(fcParam.PointOnFace) || hyline.IsPointOnLineExtension(fcParam.PointOnFace))
				{
					pipe3 = pipe2;
				}
				else
				{
					pipe3 = YJKRevitTools.NewPipe(pipe, fcParam.ExtendPoint, fcParam.PointOnFace);
					this.SetDiameter(pipe3, diameter);
					this.ElbowConnect(pipe2, pipe3, fcParam.ExtendPoint, fcParam.ExtendPoint);
				}
				doc.Regenerate();
			}
			else
			{
				Pipe pipe4 = YJKRevitTools.NewPipe(pipe, fcParam.ExtendPoint, fcParam.TurnPoint);
				this.SetDiameter(pipe4, diameter);
				this.ElbowConnect(pipe2, pipe4, fcParam.ExtendPoint, fcParam.ExtendPoint);
				pipe3 = YJKRevitTools.NewPipe(pipe, fcParam.TurnPoint, fcParam.PointOnFace);
				this.SetDiameter(pipe3, diameter);
				this.ElbowConnect(pipe3, pipe4, fcParam.TurnPoint, fcParam.TurnPoint);
				doc.Regenerate();
			}
			return pipe3;
		}

		private Pipe CreatFanCoilOutPipe(Pipe basePipe, FanCoilParameter fcParam, bool bflex = false, double flexlength = 0.0)
		{
			double diameter = fcParam.Connector.Radius * 2.0;
			Pipe pipe = YJKRevitTools.NewPipe(basePipe, this.setEndExtendPoint(fcParam.ConnPoint, (fcParam.ExtendPoint - fcParam.ConnPoint).Normalize(), flexlength), this.setEndExtendPoint(fcParam.ExtendPoint, (fcParam.ExtendPoint - fcParam.ConnPoint).Normalize(), flexlength));
			this.SetDiameter(pipe, diameter);
			this.ConnPipeAndConnector(pipe, fcParam.Connector, bflex, flexlength);
			return pipe;
		}

		private bool ConnPipeAndConnector(Pipe pipe, Connector conn, bool bflex = false, double flexlength = 0.0)
		{
			Document document = ((FamilyInstance)conn.Owner).Document;
			int result = 1;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			if (bflex)
			{
				this.PipeConnectFamilyInstance((FamilyInstance)conn.Owner, hypiping.Piping, bflex, flexlength, document);
				return result != 0;
			}
			((hypiping.StartConnector.Origin.DistanceTo(conn.Origin) > hypiping.EndConnector.Origin.DistanceTo(conn.Origin)) ? hypiping.EndConnector.Connector : hypiping.StartConnector.Connector).ConnectTo(conn);
			return result != 0;
		}

		private XYZ setEndExtendPoint(XYZ start, XYZ Vertor, double flexlength)
		{
			return start + Vertor * UnitConvert.LengthToStd(flexlength / 1000.0);
		}

		private Pipe CreatPipe(Pipe basePipe, XYZ startPnt, XYZ endPnt, double diameter)
		{
			Pipe pipe = YJKRevitTools.NewPipe(basePipe, startPnt, endPnt);
			this.SetDiameter(pipe, diameter);
			return pipe;
		}

		private IList<FanCoilParameter> GetElementOutFirst(Autodesk.Revit.DB.Document doc, IList<FanCoilParameter> listElem, int iPosition)
		{
			IList<FanCoilParameter> list = new List<FanCoilParameter>();
			for (int i = iPosition; i < listElem.Count; i++)
			{
				if (i < listElem.Count)
				{
					if (listElem[i].IndexOfElement <= 1)
					{
						break;
					}
					list.Add(listElem[i]);
				}
			}
			return list;
		}

		public IList<Pipe> ElbowSlopePipeConnector(Pipe pipe1, Pipe pipe2, ref string connMSG)
		{
			IList<Pipe> result = null;
			if (pipe1 == null || pipe2 == null)
			{
				connMSG = "传入水管不能为空！";
				return result;
			}
			Document document = pipe1.Document;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe1);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe1);
			XYZ origin3 = hypiping2.StartConnector.Origin;
			XYZ origin4 = hypiping2.EndConnector.Origin;
			YJKLine hyline = new YJKLine(new XYZ(origin.X, origin.Y, 0.0), new XYZ(origin2.X, origin2.Y, 0.0));
			hyline.MakeUnBound();
			YJKLine hyline2 = new YJKLine(new XYZ(origin3.X, origin3.Y, 0.0), new XYZ(origin4.X, origin4.Y, 0.0));
			hyline2.MakeUnBound();
			XYZ xyz = hyline.IntersectionPoint(hyline2.Line, 0.032808398950131233);
			if (xyz == null)
			{
				connMSG = "传入水管没有交点，不能进行弯头连接！";
				return result;
			}
			YJKLine hyline3 = new YJKLine(origin, origin2);
			hyline3.MakeUnBound();
			YJKLine hyline4 = new YJKLine(origin3, origin4);
			hyline4.MakeUnBound();
			XYZ project = hyline3.GetProject(xyz);
			XYZ project2 = hyline4.GetProject(xyz);
			double num = project.DistanceTo(project2);
			Connector connector = (origin.DistanceTo(project) > origin2.DistanceTo(project)) ? hypiping.EndConnector.Connector : hypiping.StartConnector.Connector;
			Connector connector2 = (origin3.DistanceTo(project2) > origin4.DistanceTo(project2)) ? hypiping2.EndConnector.Connector : hypiping2.StartConnector.Connector;
			double diameter = hypiping.GetDiameter();
			double diameter2 = hypiping2.GetDiameter();
			if (num <= 0.016404199475065617)
			{
				document.Create.NewElbowFitting(connector, connector2);
				return new List<Pipe>
				{
					pipe1,
					pipe2
				};
			}
			if (num <= diameter / 2.0 + diameter2 / 2.0)
			{
				XYZ startPnt = (origin.DistanceTo(project) > origin2.DistanceTo(project)) ? origin : origin2;
				YJKRevitTools.ChangePipeSlop(ref pipe1, startPnt, project2);
				document.Regenerate();
				this.ElbowConnect(pipe1, pipe2, project2, project2);
				return new List<Pipe>
				{
					pipe1,
					pipe2
				};
			}
			if (num <= diameter + diameter2)
			{
				connMSG = "两水管间距离不合适，不能进弯头连接！";
				return result;
			}
			Pipe pipe3 = document.CreatHYPipe(this.GetPipeSystemType(pipe1), this.GetPipeType(pipe1), document.ActiveView.GenLevel, project, project2, diameter);
			document.Regenerate();
			this.ElbowConnect(pipe3, pipe1, project, project);
			this.ElbowConnect(pipe3, pipe2, project2, project2);
			return new List<Pipe>
			{
				pipe1,
				pipe2
			};
		}

		public IList<Pipe> TeeSlopePipeConnector(Pipe pipe1, Pipe pipe2, Pipe pipe3, ref string connMSG)
		{
			IList<Pipe> result = null;
			IList<Pipe> list = new List<Pipe>();
			list.Add(pipe1);
			list.Add(pipe2);
			list.Add(pipe3);
			ConnectPipeManage connectPipeManage = new ConnectPipeManage();
			string familySymbolName = connectPipeManage.GetFamilySymbolName(ConnectPipeStyle.Tee);
			FamilySymbol familySymbolByName = connectPipeManage.GetFamilySymbolByName(familySymbolName, pipe1.Document, ref connMSG);
			this.NewTeeFittingConn(list, familySymbolByName, ref connMSG);
			return result;
		}

		private PipingSystemType GetPipeSystemType(Pipe pipe)
		{
			PipingSystem pipingSystem = pipe.MEPSystem as PipingSystem;
			return pipe.Document.GetElementById(pipingSystem.GetTypeId()) as PipingSystemType;
		}

		private PipeType GetPipeType(Pipe pipe)
		{
			return pipe.PipeType;
		}

		private XYZ ConnectDuctTerminalLoop1(Autodesk.Revit.DB.Document doc, ref Pipe pipe, IList<AutoConnectParameter> listParam, XYZ endPoint, int terminalConnType, XYZ origin)
		{
			Pipe pipe2 = pipe;
			XYZ xyz = endPoint;
			foreach (AutoConnectParameter autoConnectParameter in listParam)
			{
				XYZ xyz2 = xyz;
				Pipe pipe3 = pipe2;
				xyz = xyz2;
				pipe2 = pipe3;
			}
			doc.Regenerate();
			pipe = pipe2;
			return xyz;
		}

		private XYZ ConnectDuctTerminalLoop3(Autodesk.Revit.DB.Document doc, ref Pipe pipe, IList<AutoConnectParameter> listParam, XYZ endPoint, int terminalConnType, XYZ globePnt, XYZ direction = null)
		{
			Pipe pipe2 = pipe;
			int num = 0;
			for (int i = 0; i < listParam.Count; i++)
			{
				if (listParam[0].IndexOfElement == listParam[i].IndexOfElement)
				{
					num = i;
				}
			}
			if (num != 0)
			{
				doc.GetElementById(listParam[0].ElementId);
				doc.GetElementById(listParam[num].ElementId);
				object obj = null;
				new ConnectHelper();
				new List<AutoConnectParameter>();
				Pipe pipe3 = ((IList<Pipe>)obj)[0];
				Pipe pipe4 = ((IList<Pipe>)obj)[1];
				if (num <= 1 && num + 1 < listParam.Count)
				{
				}
			}
			else if (num == 0)
			{
				new ConnectHelper();
			}
			pipe = pipe2;
			return endPoint;
		}

		public PipeSystemType GetConnPipeSystemType(Pipe pipe, FamilyInstance fi)
		{
			Connector pipeConn = this.GetPipeConnectorEnd(pipe)[0];
			Connector matchedConnector = this.GetMatchedConnector(pipeConn, fi);
			if (matchedConnector == null)
			{
				return 0;
			}
			return matchedConnector.PipeSystemType;
		}

		private Pipe CheckDuctLength(Pipe pipe, IList<AutoConnectParameter> orderByParam)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			List<XYZ> list = new List<XYZ>();
			List<XYZ> list2 = new List<XYZ>();
			List<XYZ> list3 = new List<XYZ>();
			foreach (AutoConnectParameter autoConnectParameter in orderByParam)
			{
				if (autoConnectParameter.TurnPoint != null)
				{
					list.Add(hypiping.GetProject(autoConnectParameter.TurnPoint));
					list2.Add(hypiping.GetLineProject(autoConnectParameter.TurnPoint));
				}
				else
				{
					list.Add(hypiping.GetProject(autoConnectParameter.ExtendPoint));
					list2.Add(hypiping.GetLineProject(autoConnectParameter.ExtendPoint));
				}
			}
			bool flag = false;
			foreach (XYZ xyz in list2)
			{
				flag = false;
				using (List<XYZ>.Enumerator enumerator3 = list.GetEnumerator())
				{
					while (enumerator3.MoveNext())
					{
						if (enumerator3.Current.IsAlmostEqualTo(xyz))
						{
							flag = true;
						}
					}
				}
				if (!flag)
				{
					list3.Add(xyz);
				}
			}
			Pipe result;
			if (list3.Count > 0)
			{
				XYZ xyz2 = hypiping.StartConnector.Origin;
				XYZ xyz3 = hypiping.EndConnector.Origin;
				XYZ xyz4 = (xyz3 - xyz2).Normalize();
				foreach (XYZ xyz5 in list3)
				{
					if (xyz2.DistanceTo(xyz5) < xyz3.DistanceTo(xyz5))
					{
						if ((xyz2 - xyz5).Normalize().IsAlmostEqualTo(xyz4))
						{
							xyz2 = xyz5;
						}
					}
					else if ((xyz5 - xyz3).Normalize().IsAlmostEqualTo(xyz4))
					{
						xyz3 = xyz5;
					}
				}
				result = hypiping.Extend(xyz2, xyz3);
			}
			else
			{
				result = pipe;
			}
			return result;
		}

		private Pipe CheckDuctLength(Pipe pipe, IList<FanCoilParameter> orderByParam)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			List<XYZ> list = new List<XYZ>();
			List<XYZ> list2 = new List<XYZ>();
			List<XYZ> list3 = new List<XYZ>();
			foreach (FanCoilParameter fanCoilParameter in orderByParam)
			{
				if (fanCoilParameter.TurnPoint != null)
				{
					list.Add(hypiping.GetProject(fanCoilParameter.TurnPoint));
					list2.Add(hypiping.GetLineProject(fanCoilParameter.TurnPoint));
				}
				else
				{
					list.Add(hypiping.GetProject(fanCoilParameter.ExtendPoint));
					list2.Add(hypiping.GetLineProject(fanCoilParameter.ExtendPoint));
				}
			}
			bool flag = false;
			foreach (XYZ xyz in list2)
			{
				flag = false;
				using (List<XYZ>.Enumerator enumerator3 = list.GetEnumerator())
				{
					while (enumerator3.MoveNext())
					{
						if (enumerator3.Current.IsAlmostEqualTo(xyz))
						{
							flag = true;
						}
					}
				}
				if (!flag)
				{
					list3.Add(xyz);
				}
			}
			Pipe result;
			if (list3.Count > 0)
			{
				XYZ xyz2 = hypiping.StartConnector.Origin;
				XYZ xyz3 = hypiping.EndConnector.Origin;
				XYZ xyz4 = (xyz3 - xyz2).Normalize();
				foreach (XYZ xyz5 in list3)
				{
					if (xyz2.DistanceTo(xyz5) < xyz3.DistanceTo(xyz5))
					{
						if ((xyz2 - xyz5).Normalize().IsAlmostEqualTo(xyz4))
						{
							xyz2 = xyz5;
						}
					}
					else if ((xyz5 - xyz3).Normalize().IsAlmostEqualTo(xyz4))
					{
						xyz3 = xyz5;
					}
				}
				result = hypiping.Extend(xyz2, xyz3);
			}
			else
			{
				result = pipe;
			}
			return result;
		}

		public Connector GetMatchedConnector(Connector pipeConn, FamilyInstance fi)
		{
			Connector result = null;
			foreach (Connector connector in this.GetYJKElementConnector(fi, (Autodesk.Revit.DB.Domain)3))
			{
				if (((int)connector.PipeSystemType == (int)pipeConn.PipeSystemType || ((int)connector.PipeSystemType == 22 && (int)pipeConn.PipeSystemType != 7 && (int)pipeConn.PipeSystemType != 8)) && !connector.IsConnected)
				{
					result = connector;
					break;
				}
			}
			return result;
		}

		private IList<Connector> GetYJKElementConnector(FamilyInstance elem, Domain domain)
		{
			IList<Connector> list = new List<Connector>();
			if (elem != null)
			{
				ConnectorSetIterator connectorSetIterator = elem.MEPModel.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector = obj as Connector;
					if (connector != null && connector.Domain == domain)
					{
						list.Add(connector);
					}
				}
			}
			return list;
		}

		private IList<Connector> GetPipeConnectorEnd(Pipe pipe)
		{
			IList<Connector> list = new List<Connector>();
			ConnectorSetIterator connectorSetIterator = pipe.ConnectorManager.Connectors.ForwardIterator();
			while (connectorSetIterator.MoveNext())
			{
				object obj = connectorSetIterator.Current;
				Connector connector = obj as Connector;
				if (connector != null && (int)connector.ConnectorType == 1)
				{
					list.Add(connector);
				}
			}
			return list;
		}

		public bool NewBeatchPipeEquipment(FamilyInstance familyInstance, ref IList<Pipe> pipes, string equipmentType, FamilySymbol familySymbol)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (pipes == null || pipes.Count < 1 || familyInstance == null)
				{
					throw new System.ArgumentNullException("Pipe or Equipment", "is null");
				}
				Document document = pipes[0].Document;
				IList<Connector> hyelementConnector = new ConnectHelper().GetYJKElementConnector(familyInstance, (Autodesk.Revit.DB.Domain)3);
				IList<YJKPiping<Pipe>> orderPipes = new List<YJKPiping<Pipe>>();
				new List<Pipe>();
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				orderPipes = this.PipesAndConnectorOrderByType(hyelementConnector, pipes, document, "水泵");
				bool flag = this.PipesConnect(orderPipes, document, ref pipes, false, 0.0, null);
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null && 3 != (int)subTransaction.GetStatus())
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool NewBeatchPipeEquipment(IList<FamilyInstance> familyInstances, ref IList<Pipe> pipes, string equipmentType, FamilySymbol familySymbol)
		{
			bool result = false;
			IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
			List<YJKLine> list2 = new List<YJKLine>();
			List<XYZ> list3 = new List<XYZ>();
			foreach (Pipe piping in pipes)
			{
				YJKPiping<Pipe> item = new YJKPiping<Pipe>(piping);
				list.Add(item);
			}
			YJKPiping<Pipe> hypiping = null;
			new XYZ();
			XYZ xyz = new XYZ();
			foreach (YJKPiping<Pipe> hypiping2 in list)
			{
				hypiping = hypiping2;
				XYZ origin = hypiping2.StartConnector.Origin;
				xyz = hypiping2.EndConnector.Origin;
                //origin - xyz;
				IEnumerator<YJKPiping<Pipe>> enumerator3 = list.GetEnumerator();
				{
					while (enumerator3.MoveNext())
					{
						YJKPiping<Pipe> hypiping3 = enumerator3.Current;
						if (!hypiping2.IsParallel(hypiping3.Piping))
						{
							throw new Exception("多个设备批量连接时，所有水管须平行");
						}
						YJKLine item2 = new YJKLine(hypiping3.StartConnector.Origin, hypiping3.EndConnector.Origin);
						list2.Add(item2);
						if (!hypiping3.StartConnector.Connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(hypiping2.StartConnector.Connector.CoordinateSystem.BasisZ))
						{
							list3.Add(hypiping3.StartConnector.Origin);
						}
						else
						{
							list3.Add(hypiping3.EndConnector.Origin);
						}
					}
					break;
				}
			}
			IList<EquipmentCoilDouble> list4 = new List<EquipmentCoilDouble>();
			for (int i = 0; i < familyInstances.Count; i++)
			{
				if (familyInstances.Count == 1)
				{
					list4.Add(new EquipmentCoilDouble(familyInstances[i], null, false, this.PointToFiDistance(familyInstances[i], hypiping)));
				}
				else if (i >= familyInstances.Count - 1)
				{
					break;
				}
				bool flag = false;
				for (int j = i + 1; j < familyInstances.Count; j++)
				{
					if (this.IsAlignEquipment(familyInstances[i], familyInstances[j], hypiping))
					{
						list4.Add(new EquipmentCoilDouble(familyInstances[i], familyInstances[j], true, this.PointToFiDistance(familyInstances[i], hypiping)));
						flag = true;
						break;
					}
				}
				if (!flag && !this.IsExistInEquipmentDouble(list4, familyInstances[i]))
				{
					list4.Add(new EquipmentCoilDouble(familyInstances[i], null, false, this.PointToFiDistance(familyInstances[i], hypiping)));
				}
				if (!flag && i == familyInstances.Count - 2 && !this.IsExistInEquipmentDouble(list4, familyInstances[i + 1]))
				{
					list4.Add(new EquipmentCoilDouble(familyInstances[i + 1], null, false, this.PointToFiDistance(familyInstances[i + 1], hypiping)));
				}
			}
			list4 = (from c in list4
			orderby c.Position
			select c).ToList<EquipmentCoilDouble>();
			foreach (EquipmentCoilDouble equipmentCoilDouble in list4)
			{
				if (equipmentCoilDouble.IsDouble)
				{
					list.Clear();
					foreach (Pipe piping2 in pipes)
					{
						list.Add(new YJKPiping<Pipe>(piping2));
					}
					IList<YJKPiping<Pipe>> orderPipes = this.EquipmentConnect(equipmentCoilDouble, list, null);
					this.PipesConnect(orderPipes, (equipmentCoilDouble.ElemFirst as Element).Document, ref pipes, false, 0.0, null);
				}
				else
				{
					this.NewBeatchPipeEquipment(equipmentCoilDouble.ElemFirst as FamilyInstance, ref pipes, equipmentType, familySymbol);
				}
				IList<Pipe> list5 = new List<Pipe>();
				foreach (YJKLine hyline in list2)
				{
					foreach (Pipe pipe in pipes)
					{
						bool flag2 = false;
						YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipe);
						if (hyline.IsLineOnLine(hypiping4.GetLocationCurve() as Line))
						{
							foreach (XYZ xyz2 in list3)
							{
								if (xyz2.IsAlmostEqualTo(hypiping4.StartConnector.Origin) || xyz2.IsAlmostEqualTo(hypiping4.EndConnector.Origin))
								{
									list5.Add(pipe);
									flag2 = true;
									break;
								}
							}
							if (flag2)
							{
								break;
							}
						}
					}
				}
				if (list5.Count == list2.Count)
				{
					pipes = list5;
				}
				else if (list5.Count > 0)
				{
					int count = list5.Count;
					int count2 = list2.Count;
				}
				result = true;
			}
			return result;
		}

		private FamilyInstance MirrorUpDownDrant(FamilyInstance familyInstance, IList<Pipe> pipes)
		{
			Transform transform = familyInstance.GetTransform();
			Element element = familyInstance;
			Document document = familyInstance.Document;
			BoundingBoxXYZ boundingBoxXYZ = element.get_BoundingBox(familyInstance.Document.ActiveView);
			Outline outline;
			if (transform != null && transform.IsTranslation)
			{
				outline = new Outline(transform.OfPoint(boundingBoxXYZ.Min), transform.OfPoint(boundingBoxXYZ.Max));
			}
			else
			{
				outline = new Outline(boundingBoxXYZ.Min, boundingBoxXYZ.Max);
			}
			BoundingBoxIntersectsFilter boundingBoxIntersectsFilter = new BoundingBoxIntersectsFilter(outline);
			List<Element> list = new FilteredElementCollector(familyInstance.Document).OfClass(typeof(Wall)).WherePasses(boundingBoxIntersectsFilter).ToElements().ToList<Element>();
			foreach (LinkedDocument linkedDocument in familyInstance.Document.GetLinkedDocuments())
			{
				list.AddRange(new FilteredElementCollector(linkedDocument.Document).OfClass(typeof(Wall)).WherePasses(boundingBoxIntersectsFilter).ToElements());
			}
			if (list != null && list.Count > 0)
			{
				GeometryElement geometryElement = (list.FirstOrDefault<Element>() as Wall).get_Geometry(new Options
				{
                    DetailLevel = (ViewDetailLevel)3
				});
				bool flag = true;
				foreach (GeometryObject geometryObject in geometryElement)
				{
					if (geometryObject is Solid)
					{
						foreach (object obj in ((Solid)geometryObject).Faces)
						{
							IntersectionResult intersectionResult = ((PlanarFace)obj).Project(((LocationPoint)familyInstance.Location).Point);
							if (intersectionResult != null && intersectionResult.Distance < Math.Abs(0.001))
							{
								flag = false;
								break;
							}
						}
					}
				}
				if (flag)
				{
					XYZ xyz = null;
					foreach (object obj2 in ((Solid)geometryElement.FirstOrDefault<GeometryObject>()).Faces)
					{
						PlanarFace planarFace = (PlanarFace)obj2;
						if (planarFace.FaceNormal().IsAlmostEqualTo(familyInstance.FacingOrientation) && planarFace.Project(((LocationPoint)familyInstance.Location).Point) != null)
						{
							xyz = planarFace.Project(((LocationPoint)familyInstance.Location).Point).XYZPoint;
						}
					}
					if (xyz != null)
					{
						Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(familyInstance.FacingOrientation, (xyz + ((LocationPoint)familyInstance.Location).Point) / 2.0);
						List<ElementId> list2 = new List<ElementId>();
						list2.Add(familyInstance.Id);
						list2 = RevitVersionFuncs.MirrorElements(familyInstance.Document, list2, plane, BuiltInCategory.OST_MechanicalEquipment, false);
						if (list2 != null && list2.Count > 0)
						{
							familyInstance = (document.GetElement(list2.FirstOrDefault<ElementId>()) as FamilyInstance);
						}
					}
				}
			}
			return familyInstance;
		}

		private FamilyInstance MirrorDrant(FamilyInstance familyInstance, string connectorType, IList<Pipe> pipes)
		{
			Document document = familyInstance.Document;
			IList<Connector> hyelementConnector = new ConnectHelper().GetYJKElementConnector(familyInstance, (Autodesk.Revit.DB.Domain)3);
			Connector connector = hyelementConnector[0];
			Connector connector2 = hyelementConnector[1];
			Pipe pipe = pipes[0];
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			if (hyelementConnector[0].Origin.Z > hyelementConnector[1].Origin.Z)
			{
				connector = hyelementConnector[0];
				Connector connector3 = hyelementConnector[1];
			}
			else
			{
				connector = hyelementConnector[1];
				Connector connector4 = hyelementConnector[0];
			}
			if (connectorType == "1")
			{
				if (connector.IsConnected)
				{
					return familyInstance;
				}
				familyInstance = this.MirrorUpDownDrant(familyInstance, pipes);
				XYZ xyz = connector.CoordinateSystem.BasisZ.Normalize();
				if (YJKRevitTools.isRiserPipe(pipe))
				{
					XYZ project = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin).GetProject(connector.Origin);
					if (xyz.IsAlmostEqualTo((connector.Origin - project).Normalize()))
					{
						return familyInstance;
					}
					XYZ connectorDirectionPoint = this.GetConnectorDirectionPoint(connector, 0.5);
					XYZ xyz2 = (project - connectorDirectionPoint).Normalize();
					if (xyz.AngleTo(xyz2) / Math.PI * 180.0 > 90.0)
					{
						Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(familyInstance.HandOrientation, ((LocationPoint)familyInstance.Location).Point);
						List<ElementId> list = new List<ElementId>();
						list.Add(familyInstance.Id);
						list = RevitVersionFuncs.MirrorElements(familyInstance.Document, list, plane, BuiltInCategory.OST_MechanicalEquipment, false);
						if (list != null && list.Count > 0)
						{
							familyInstance = (document.GetElement(list.FirstOrDefault<ElementId>()) as FamilyInstance);
						}
					}
				}
			}
			else
			{
				familyInstance = this.MirrorUpDownDrant(familyInstance, pipes);
			}
			return familyInstance;
		}

		public bool NewBeatchPipeEquipment(FamilyInstance familyInstance, ref IList<Pipe> pipes, string equipmentType, string connectorType = "1")
		{
			bool flag = false;
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				familyInstance = this.MirrorDrant(familyInstance, connectorType, pipes);
				if (pipes == null || pipes.Count < 1 || familyInstance == null)
				{
					throw new System.ArgumentNullException("Pipe or Equipment", "is null");
				}
				Document document = pipes[0].Document;
				ConnectHelper connectHelper = new ConnectHelper();
				YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
				string text = "";
				FamilySymbol familySymbolByName = YJKRevitTools.GetFamilySymbolByName("Fitting", "水管三通", document, ref text);
				bool isSSTee = false;
				IList<Connector> hyelementConnector = connectHelper.GetYJKElementConnector(familyInstance, (Autodesk.Revit.DB.Domain)3);
				new List<YJKPiping<Pipe>>();
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				Connector connector = hyelementConnector[0];
				Connector connector2 = hyelementConnector[1];
				double num = 0.5;
				Pipe pipe = pipes[0];
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				if (hyelementConnector[0].Origin.Z > hyelementConnector[1].Origin.Z)
				{
					connector = hyelementConnector[0];
					connector2 = hyelementConnector[1];
				}
				else
				{
					connector = hyelementConnector[1];
					connector2 = hyelementConnector[0];
				}
				if (connectorType == "1")
				{
					if (connector.IsConnected)
					{
						YJKMessageBox.Show("侧边的进口已经连接，请选择底边的进口");
						subTransaction.RollBack();
						subTransaction = null;
						return true;
					}
					XYZ xyz = connector.CoordinateSystem.BasisZ.Normalize();
					if (YJKRevitTools.isRiserPipe(pipe))
					{
						XYZ project = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin).GetProject(connector.Origin);
						if (xyz.IsAlmostEqualTo((connector.Origin - project).Normalize()))
						{
							YJKMessageBox.Show("选择的管道位置不合适，无法与消火栓侧边连接");
							subTransaction.RollBack();
							subTransaction = null;
							return true;
						}
						XYZ connectorDirectionPoint = this.GetConnectorDirectionPoint(connector, 0.5);
						XYZ xyz2 = (project - connectorDirectionPoint).Normalize();
						if (xyz.AngleTo(xyz2) / Math.PI * 180.0 >= 90.0)
						{
							YJKMessageBox.Show("选择的管道位置不合适，无法与消火栓侧边连接");
							subTransaction.RollBack();
							subTransaction = null;
							return true;
						}
						Pipe pipe2 = YJKRevitTools.NewPipe(pipe, connectorDirectionPoint, connector.Origin);
						pipe2.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(connector.Radius * 2.0);
						this.PipeConnectFamilyInstance(familyInstance, pipe2);
						document.Regenerate();
						string a = "";
						hypipeConnectorHelper.TPipeConnectRPipe(pipe, pipe2, document, familySymbolByName, isSSTee, 2, ref a, false);
						if (a != "")
						{
							a = "";
							hypipeConnectorHelper.TPipeConnectRPipe(pipe, pipe2, document, familySymbolByName, isSSTee, ref a, false, 0.0);
							flag = !(a != "");
						}
						else
						{
							flag = true;
						}
					}
					else
					{
						if (hypiping.StartConnector.Origin.Z <= connector.Origin.Z - 0.8)
						{
							num = -num;
						}
						else if (hypiping.StartConnector.Origin.Z < connector.Origin.Z + 0.8 && hypiping.StartConnector.Origin.Z > connector.Origin.Z - 0.8)
						{
							YJKMessageBox.Show("选择的管道位置不合适，无法与消火栓侧边连接");
							subTransaction.RollBack();
							subTransaction = null;
							return true;
						}
						XYZ project2 = new YJKLine(new XYZ(hypiping.StartConnector.Origin.X, hypiping.StartConnector.Origin.Y, connector.Origin.Z), new XYZ(hypiping.EndConnector.Origin.X, hypiping.EndConnector.Origin.Y, connector.Origin.Z)).GetProject(connector.Origin);
						if (YJKRevitTools.IsPlumbVector(xyz, hypiping.StartConnector.Origin, hypiping.EndConnector.Origin) && xyz.IsAlmostEqualTo((project2 - connector.Origin).Normalize()))
						{
							Pipe pipe3 = YJKRevitTools.NewPipe(hypiping.Piping, project2, connector.Origin);
							this.PipeConnectFamilyInstance(familyInstance, pipe3);
							this.PipeSetParameter(connector, pipe3);
							XYZ startXYZ = new XYZ(project2.X, project2.Y, project2.Z + num);
							Pipe pipe4 = YJKRevitTools.NewPipe(hypiping.Piping, startXYZ, project2);
							pipe4.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(connector.Radius * 2.0);
							document.Regenerate();
							string text2 = "";
							hypipeConnectorHelper.TPipeConnectRPipe(pipe4, pipe, document, familySymbolByName, isSSTee, 1, ref text2, false);
							hypipeConnectorHelper.TPipeConnectRPipe(pipe4, pipe3, document, familySymbolByName, isSSTee, 2, ref text2, false);
							if (text2 != "")
							{
								YJKMessageBox.Show(text2);
							}
							else
							{
								flag = true;
							}
						}
						else
						{
							XYZ connectorDirectionPoint2 = this.GetConnectorDirectionPoint(connector, 0.5);
							Pipe pipe5 = YJKRevitTools.NewPipe(hypiping.Piping, connectorDirectionPoint2, connector.Origin);
							pipe5.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(connector.Radius * 2.0);
							this.PipeConnectFamilyInstance(familyInstance, pipe5);
							this.PipeSetParameter(connector, pipe5);
							XYZ startXYZ2 = new XYZ(connectorDirectionPoint2.X, connectorDirectionPoint2.Y, connectorDirectionPoint2.Z + num);
							Pipe pipe6 = YJKRevitTools.NewPipe(hypiping.Piping, startXYZ2, connectorDirectionPoint2);
							pipe6.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(connector.Radius * 2.0);
							document.Regenerate();
							string text3 = "";
							hypipeConnectorHelper.TPipeConnectRPipe(pipe6, pipe5, document, familySymbolByName, isSSTee, 2, ref text3, false);
							hypipeConnectorHelper.TPipeConnectRPipe(pipe6, pipe, document, familySymbolByName, isSSTee, 1, ref text3, false);
							if (text3 != "")
							{
								YJKMessageBox.Show(text3);
							}
							else
							{
								flag = true;
							}
						}
					}
				}
				else
				{
					if (connector2.IsConnected)
					{
						YJKMessageBox.Show("底边的进口已经连接，请选择侧边的进口");
						subTransaction.RollBack();
						subTransaction = null;
						return true;
					}
					connector2.CoordinateSystem.BasisZ.Normalize();
					XYZ xyz3 = connector.CoordinateSystem.BasisZ.Normalize();
					XYZ connectorDirectionPoint3 = this.GetConnectorDirectionPoint(connector2, 0.5);
					Pipe pipe7 = YJKRevitTools.NewPipe(hypiping.Piping, connector2.Origin, connectorDirectionPoint3);
					pipe7.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(connector2.Radius * 2.0);
					this.PipeConnectFamilyInstance(familyInstance, pipe7);
					if (YJKRevitTools.isRiserPipe(pipe))
					{
						XYZ project3 = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin).GetProject(connectorDirectionPoint3);
						XYZ xyz4 = (project3 - connectorDirectionPoint3).Normalize();
						Pipe pipe8;
						if (0.0 < xyz3.AngleTo(xyz4) / Math.PI * 180.0 && xyz3.AngleTo(xyz4) / Math.PI * 180.0 < 88.0)
						{
							XYZ xyz5 = xyz3;
							XYZ endXYZ = connectorDirectionPoint3 + 0.5 * xyz5;
							pipe8 = YJKRevitTools.NewPipe(hypiping.Piping, connectorDirectionPoint3, endXYZ);
							pipe8.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(connector2.Radius * 2.0);
						}
						else if (xyz3.AngleTo(xyz4) / Math.PI * 180.0 > 92.0)
						{
							XYZ xyz5 = -xyz3;
							XYZ endXYZ2 = connectorDirectionPoint3 + 0.5 * xyz5;
							pipe8 = YJKRevitTools.NewPipe(hypiping.Piping, connectorDirectionPoint3, endXYZ2);
							pipe8.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(connector2.Radius * 2.0);
						}
						else
						{
							if (xyz3.AngleTo(xyz4) / Math.PI * 180.0 != 0.0 && (xyz3.AngleTo(xyz4) / Math.PI * 180.0 > 92.0 || xyz3.AngleTo(xyz4) / Math.PI * 180.0 < 88.0))
							{
								YJKMessageBox.Show("选择的管道位置不合适，无法与消火栓底边连接");
								subTransaction.RollBack();
								subTransaction = null;
								return true;
							}
							pipe8 = YJKRevitTools.NewPipe(hypiping.Piping, connectorDirectionPoint3, project3);
							pipe8.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(connector2.Radius * 2.0);
						}
						document.Regenerate();
						string text4 = "";
						hypipeConnectorHelper.TPipeConnectRPipe(pipe7, pipe8, document, familySymbolByName, isSSTee, 1, ref text4, false);
						if (text4 != "")
						{
							YJKMessageBox.Show(text4);
						}
						else
						{
							flag = true;
						}
						text4 = "";
						hypipeConnectorHelper.TPipeConnectRPipe(pipe, pipe8, document, familySymbolByName, isSSTee, 2, ref text4, false);
						if (text4 != "")
						{
							text4 = "";
							hypipeConnectorHelper.TPipeConnectRPipe(pipe, pipe8, document, familySymbolByName, isSSTee, ref text4, false, 0.0);
						}
						else
						{
							flag = true;
						}
					}
					else
					{
						if (hypiping.StartConnector.Origin.Z <= connector2.Origin.Z - 0.8)
						{
							num = -num;
						}
						else if (hypiping.StartConnector.Origin.Z > connector2.Origin.Z - 0.8 && hypiping.StartConnector.Origin.Z < connector2.Origin.Z + 0.8)
						{
							YJKMessageBox.Show("选择的管道位置不合适，无法与消火栓底边连接");
							subTransaction.RollBack();
							subTransaction = null;
							return true;
						}
						YJKLine hyline = new YJKLine(new XYZ(hypiping.StartConnector.Origin.X, hypiping.StartConnector.Origin.Y, connectorDirectionPoint3.Z), new XYZ(hypiping.EndConnector.Origin.X, hypiping.EndConnector.Origin.Y, connectorDirectionPoint3.Z));
						hyline.MakeUnBound();
						XYZ project4 = hyline.GetProject(connectorDirectionPoint3);
						XYZ xyz6 = (project4 - connectorDirectionPoint3).Normalize();
						if (YJKRevitTools.IsPlumbVector(xyz3, hypiping.StartConnector.Origin, hypiping.EndConnector.Origin))
						{
							Pipe pipe9 = YJKRevitTools.NewPipe(hypiping.Piping, project4, connectorDirectionPoint3);
							pipe9.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(connector2.Radius * 2.0);
							XYZ endXYZ3 = new XYZ(project4.X, project4.Y, project4.Z + num);
							Pipe pipe10 = YJKRevitTools.NewPipe(hypiping.Piping, project4, endXYZ3);
							pipe10.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(connector2.Radius * 2.0);
							document.Regenerate();
							string text5 = "";
							hypipeConnectorHelper.TPipeConnectRPipe(pipe7, pipe9, document, familySymbolByName, isSSTee, 1, ref text5, false);
							hypipeConnectorHelper.TPipeConnectRPipe(pipe10, pipe9, document, familySymbolByName, isSSTee, 2, ref text5, false);
							hypipeConnectorHelper.TPipeConnectRPipe(pipe10, pipe, document, familySymbolByName, isSSTee, 1, ref text5, false);
							if (text5 != "")
							{
								YJKMessageBox.Show(text5);
							}
							else
							{
								flag = true;
							}
						}
						else
						{
							XYZ xyz7 = xyz3;
							if (xyz3.AngleTo(xyz6) / Math.PI * 180.0 < 90.0)
							{
								xyz7 = xyz3;
							}
							else if (xyz3.AngleTo(xyz6) / Math.PI * 180.0 > 90.0)
							{
								xyz7 = -xyz3;
							}
							XYZ xyz8 = connectorDirectionPoint3 + 2.3 * xyz7;
							Pipe pipe11 = YJKRevitTools.NewPipe(hypiping.Piping, connectorDirectionPoint3, xyz8);
							pipe11.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(connector2.Radius * 2.0);
							XYZ endXYZ4 = new XYZ(xyz8.X, xyz8.Y, xyz8.Z + num);
							Pipe pipe12 = YJKRevitTools.NewPipe(hypiping.Piping, xyz8, endXYZ4);
							pipe12.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(connector2.Radius * 2.0);
							document.Regenerate();
							string text6 = "";
							hypipeConnectorHelper.TPipeConnectRPipe(pipe7, pipe11, document, familySymbolByName, isSSTee, 1, ref text6, false);
							hypipeConnectorHelper.TPipeConnectRPipe(pipe12, pipe11, document, familySymbolByName, isSSTee, 2, ref text6, false);
							hypipeConnectorHelper.TPipeConnectRPipe(pipe12, pipe, document, familySymbolByName, isSSTee, 1, ref text6, false);
							if (text6 != "")
							{
								YJKMessageBox.Show(text6);
							}
							else
							{
								flag = true;
							}
						}
					}
				}
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null && 3 != (int)subTransaction.GetStatus())
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private IList<YJKPiping<Pipe>> PipesAndConnectorOrderByType(IList<Connector> connectors, IList<Pipe> pipes, Autodesk.Revit.DB.Document doc, bool bFlex, double flexLength, int eqType = 0)
		{
			IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
			if (connectors == null || connectors.Count < 1 || pipes == null || pipes.Count < 1)
			{
				return null;
			}
			IList<Pipe> list2 = new List<Pipe>();
			foreach (Pipe item in pipes)
			{
				list2.Add(item);
			}
			foreach (Connector connector in connectors)
			{
				FamilyInstance fi = connector.Owner as FamilyInstance;
				foreach (Pipe pipe in list2)
				{
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
                    if ((connector.PipeSystemType == hypiping.GetConnectorPipingSystemType().pipeSystemType || ((int)connector.PipeSystemType == 22 && (int)hypiping.GetConnectorPipingSystemType().pipeSystemType != 7 && (int)hypiping.GetConnectorPipingSystemType().pipeSystemType != 8)) && !connector.IsConnected)
					{
						double distance = 0.2;
						if (eqType == 1)
						{
							distance = this.GetDistanceSystemTypeSet(connector.PipeSystemType);
						}
						if ((int)connector.PipeSystemType != 7 && (int)connector.PipeSystemType != 8)
						{
							bFlex = false;
						}
						Pipe pipe2 = YJKRevitTools.NewPipe(hypiping.Piping, this.GetConnectorDirectionPoint(connector, distance, bFlex, flexLength), this.GetFlexConnectorDirectionPoint(connector, bFlex, flexLength));
						this.PipeSetParameter(connector, pipe2);
						doc.Regenerate();
						this.PipeConnectFamilyInstance(fi, pipe2, bFlex, flexLength, doc);
						if (eqType != 1)
						{
							list.Add(hypiping);
							list.Add(new YJKPiping<Pipe>(pipe2));
							list2.Remove(pipe);
							break;
						}
						bool flag = false;
						YJKPiping<Pipe> item2 = this.CheckPipingsAngleOrCanConnect(hypiping, new YJKPiping<Pipe>(pipe2), doc, ref flag);
						if (!flag)
						{
							return null;
						}
						list.Add(hypiping);
						list.Add(item2);
						list2.Remove(pipe);
						break;
					}
				}
			}
			return list;
		}

		private IList<YJKPiping<Pipe>> PipesAndConnectorOrderByType(IList<Connector> connectors, IList<Pipe> pipes, Autodesk.Revit.DB.Document doc, int eqType = 0)
		{
			IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
			IList<Pipe> list2 = new List<Pipe>();
			foreach (Pipe item in pipes)
			{
				list2.Add(item);
			}
			foreach (Connector connector in connectors)
			{
				XYZ origin = connector.Origin;
				FamilyInstance fi = connector.Owner as FamilyInstance;
				foreach (Pipe pipe in list2)
				{
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
                    if ((connector.PipeSystemType == hypiping.GetConnectorPipingSystemType().pipeSystemType || ((int)connector.PipeSystemType == 22 && (int)hypiping.GetConnectorPipingSystemType().pipeSystemType != 7 && (int)hypiping.GetConnectorPipingSystemType().pipeSystemType != 8)) && !connector.IsConnected)
					{
						double distance = 0.2;
						if (eqType == 1)
						{
							distance = this.GetDistanceSystemTypeSet(connector.PipeSystemType);
						}
						XYZ connectorDirectionPoint = this.GetConnectorDirectionPoint(connector, distance);
						Pipe pipe2 = YJKRevitTools.NewPipe(hypiping.Piping, connectorDirectionPoint, origin);
						this.PipeSetParameter(connector, pipe2);
						doc.Regenerate();
						this.PipeConnectFamilyInstance(fi, pipe2);
						if (eqType != 1)
						{
							list.Add(hypiping);
							list.Add(new YJKPiping<Pipe>(pipe2));
							list2.Remove(pipe);
							break;
						}
						bool flag = false;
						YJKPiping<Pipe> item2 = this.CheckPipingsAngleOrCanConnect(hypiping, new YJKPiping<Pipe>(pipe2), doc, ref flag);
						if (!flag)
						{
							return null;
						}
						list.Add(hypiping);
						list.Add(item2);
						list2.Remove(pipe);
						break;
					}
				}
			}
			return list;
		}

		public bool ValveEquipmentConn(FamilyInstance fi, Autodesk.Revit.DB.Document doc, IList<Pipe> pipes, double distance, List<FamilySymbol> fsList)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (pipes == null || pipes.Count < 1 || fi == null)
				{
					throw new System.ArgumentNullException("Pipe or Equipment", "is null");
				}
				subTransaction = new SubTransaction(doc);
				subTransaction.Start();
				IList<Connector> hyelementConnector = new ConnectHelper().GetYJKElementConnector(fi, (Autodesk.Revit.DB.Domain)3);
				IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
				IList<Pipe> list2 = new List<Pipe>();
				new List<Connector>();
				new List<Connector>();
				foreach (Pipe item in pipes)
				{
					list2.Add(item);
				}
				foreach (Connector connector in hyelementConnector)
				{
					foreach (Pipe pipe in list2)
					{
						YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
						if ((connector.PipeSystemType == hypiping.GetConnectorPipingSystemType().pipeSystemType || 
                            ((int)connector.PipeSystemType == 22 &&
                            (int)hypiping.GetConnectorPipingSystemType().pipeSystemType != 7 &&
                            (int)hypiping.GetConnectorPipingSystemType().pipeSystemType != 8)) && 
                            !connector.IsConnected)
						{
							double distanceSystemTypeSet = this.GetDistanceSystemTypeSet(connector.PipeSystemType);
							XYZ connectorDirectionPoint = this.GetConnectorDirectionPoint(connector, distanceSystemTypeSet);
							YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(YJKRevitTools.NewPipe(hypiping.Piping, connector, connectorDirectionPoint));
							if (hypiping2.StartConnector.Origin.IsAlmostEqualTo(connector.Origin))
							{
								hypiping2.StartConnector.Connector.ConnectTo(connector);
							}
							else
							{
								hypiping2.EndConnector.Connector.ConnectTo(connector);
							}
							List<FamilySymbol> list3 = new List<FamilySymbol>();
							if ((int)connector.PipeSystemType == 7)
							{
								if (fsList[0] != null)
								{
									list3.Add(fsList[0]);
								}
								if (fsList[1] != null)
								{
									list3.Add(fsList[1]);
								}
							}
							else if ((int)connector.PipeSystemType == 8)
							{
								if (fsList[2] != null)
								{
									list3.Add(fsList[2]);
								}
								if (fsList[3] != null)
								{
									list3.Add(fsList[3]);
								}
							}
							if (list3.Count > 0)
							{
								GroupValueManage groupValueManage = new GroupValueManage(doc, hypiping2.Piping, connector.Origin, 100.0, list3);
								if (!groupValueManage.Go())
								{
									throw new Exception("阀件插入失败");
								}
								YJKPiping<Pipe> hyPipe = new YJKPiping<Pipe>(groupValueManage.GetEndPipe());
								bool flag = false;
								YJKPiping<Pipe> item2 = this.CheckPipingsAngleOrCanConnect(hypiping, hyPipe, doc, ref flag);
								if (!flag)
								{
									return false;
								}
								list.Add(hypiping);
								list.Add(item2);
							}
							else
							{
								bool flag2 = false;
								YJKPiping<Pipe> item3 = this.CheckPipingsAngleOrCanConnect(hypiping, hypiping2, doc, ref flag2);
								if (!flag2)
								{
									return false;
								}
								list.Add(hypiping);
								list.Add(item3);
							}
							list2.Remove(pipe);
							break;
						}
					}
				}
				bool flag3 = this.PipesConnect(list, doc, ref pipes, false, 0.0, null);
				subTransaction.Commit();
				result = flag3;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null && 3 != (int)subTransaction.GetStatus())
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool ValveEquipmentConn(FamilyInstance fi, Autodesk.Revit.DB.Document doc, IList<Pipe> pipes, double distance, List<FamilySymbol> fsList, bool bFlex, double flexLength)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (pipes == null || pipes.Count < 1 || fi == null)
				{
					throw new System.ArgumentNullException("Pipe or Equipment", "is null");
				}
				subTransaction = new SubTransaction(doc);
				subTransaction.Start();
				ConnectHelper connectHelper = new ConnectHelper();
				IList<Connector> conns = connectHelper.GetYJKElementConnector(fi, (Autodesk.Revit.DB.Domain)3);
				conns = this.RemoveConnedConnectors(conns);
				IList<YJKPiping<Pipe>> orderPipes = new List<YJKPiping<Pipe>>();
				Dictionary<YJKPiping<Pipe>, List<FamilySymbol>> accDic = new Dictionary<YJKPiping<Pipe>, List<FamilySymbol>>();
				if (this.ValvePipesAndConnectorOrderByType(conns, pipes, doc, bFlex, flexLength, fsList, ref orderPipes, connectHelper, ref accDic, 0))
				{
					bool flag = this.PipesConnect(orderPipes, doc, ref pipes, bFlex, flexLength, accDic);
					subTransaction.Commit();
					result = flag;
				}
				else
				{
					result = false;
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null && 3 != (int)subTransaction.GetStatus())
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private bool ValvePipesAndConnectorOrderByType(IList<Connector> conns, IList<Pipe> pipes, Autodesk.Revit.DB.Document doc, bool bFlex, double flexLength, List<FamilySymbol> fsList, ref IList<YJKPiping<Pipe>> orderPipe, ConnectHelper cm, ref Dictionary<YJKPiping<Pipe>, List<FamilySymbol>> accDic, int eqType = 0)
		{
			IList<Pipe> list = new List<Pipe>();
			if (conns == null || conns.Count < 1 || pipes == null || pipes.Count < 1)
			{
				return false;
			}
			foreach (Pipe item in pipes)
			{
				list.Add(item);
			}
			foreach (Connector connector in conns)
			{
				foreach (Pipe pipe in list)
				{
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
                    if ((connector.PipeSystemType == hypiping.GetConnectorPipingSystemType().pipeSystemType || ((int)connector.PipeSystemType == 22 && (int)hypiping.GetConnectorPipingSystemType().pipeSystemType != 7 && (int)hypiping.GetConnectorPipingSystemType().pipeSystemType != 8)) && !connector.IsConnected)
					{
						YJKPiping<Pipe> hypiping2 = null;
						this.hyPipingConnector(bFlex, flexLength, connector, hypiping, ref hypiping2, doc, cm);
						List<FamilySymbol> list2 = new List<FamilySymbol>();
						if ((int)connector.PipeSystemType == 7)
						{
							if (fsList[0] != null)
							{
								list2.Add(fsList[0]);
							}
							if (fsList[1] != null)
							{
								list2.Add(fsList[1]);
							}
						}
						else if ((int)connector.PipeSystemType == 8)
						{
							if (fsList[2] != null)
							{
								list2.Add(fsList[2]);
							}
							if (fsList[3] != null)
							{
								list2.Add(fsList[3]);
							}
						}
						if (list2.Count > 0)
						{
							GroupValueManage groupValueManage = new GroupValueManage(doc, hypiping2.Piping, connector.Origin, 100.0, list2, false);
							Pipe piping;
							if (!groupValueManage.Go())
							{
								accDic[hypiping] = list2;
								piping = hypiping2.Piping;
							}
							else
							{
								piping = groupValueManage.GetEndPipe();
							}
							YJKPiping<Pipe> hyPipe = new YJKPiping<Pipe>(piping);
							bool flag = false;
							YJKPiping<Pipe> item2 = this.CheckPipingsAngleOrCanConnect(hypiping, hyPipe, doc, ref flag);
							if (!flag)
							{
								return false;
							}
							orderPipe.Add(hypiping);
							orderPipe.Add(item2);
						}
						else
						{
							bool flag2 = false;
							YJKPiping<Pipe> item3 = this.CheckPipingsAngleOrCanConnect(hypiping, hypiping2, doc, ref flag2);
							if (!flag2)
							{
								return false;
							}
							orderPipe.Add(hypiping);
							orderPipe.Add(item3);
						}
						list.Remove(pipe);
						break;
					}
				}
			}
			return true;
		}

		private void hyPipingConnector(bool bFlex, double flexLength, Connector item, YJKPiping<Pipe> hyPipe, ref YJKPiping<Pipe> hyNewPipe, Autodesk.Revit.DB.Document doc, ConnectHelper cm)
		{
			double distanceSystemTypeSet = this.GetDistanceSystemTypeSet(item.PipeSystemType);
			if (bFlex && ((int)item.PipeSystemType == 7 || (int)item.PipeSystemType == 8))
			{
				Pipe pipe = YJKRevitTools.NewPipe(hyPipe.Piping, this.GetFlexConnectorDirectionPoint(item, bFlex, flexLength), this.GetConnectorDirectionPoint(item, distanceSystemTypeSet, bFlex, flexLength));
				this.PipeSetParameter(item, pipe);
				doc.Regenerate();
				hyNewPipe = new YJKPiping<Pipe>(pipe);
				Connector connector = null;
				FamilyInstance familyInstance = null;
				FamilySymbol ductFittingFamilySymbolByFamilyNameAndSymbol = this.GetDuctFittingFamilySymbolByFamilyNameAndSymbol("波纹管", "标准", doc);
				UnifiedModified.ActivateFamilySymbol(ductFittingFamilySymbolByFamilyNameAndSymbol);
				List<XYZ> list = new List<XYZ>();
				if (Math.Abs(hyNewPipe.StartConnector.Origin.DistanceTo(item.Origin) - UnitConvert.LengthToStd(flexLength / 1000.0)) < 0.001)
				{
					connector = hyNewPipe.StartConnector.Connector;
				}
				else
				{
					connector = hyNewPipe.EndConnector.Connector;
				}
				list.Add((connector.Origin - item.Origin) / 2.0 + item.Origin);
				foreach (object obj in cm.LayFamilyInstance(list, ductFittingFamilySymbolByFamilyNameAndSymbol, 0, doc))
				{
					familyInstance = (obj as FamilyInstance);
					familyInstance.GetParameter("波纹管长度").Set(UnitConvert.LengthToStd(flexLength / 1000.0));
				}
				this.RotateElementbyDuctVec((connector.Origin - item.Origin).Normalize(), doc, familyInstance, cm, list.FirstOrDefault<XYZ>());
				doc.Regenerate();
				cm.GetYJKElementConnector(familyInstance, (Autodesk.Revit.DB.Domain)3).GetNearestConnector(connector.Origin).ConnectTo(connector);
				cm.GetYJKElementConnector(familyInstance, (Autodesk.Revit.DB.Domain)3).GetNearestConnector(item.Origin).ConnectTo(item);
				return;
			}
			Pipe pipe2 = YJKRevitTools.NewPipe(hyPipe.Piping, item, this.GetConnectorDirectionPoint(item, distanceSystemTypeSet, bFlex, flexLength));
			this.PipeSetParameter(item, pipe2);
			doc.Regenerate();
			hyNewPipe = new YJKPiping<Pipe>(pipe2);
			if (hyNewPipe.StartConnector.Origin.IsAlmostEqualTo(item.Origin))
			{
				hyNewPipe.StartConnector.Connector.ConnectTo(item);
				return;
			}
			hyNewPipe.EndConnector.Connector.ConnectTo(item);
		}

		public bool ValveEquipmentConn(IList<FamilyInstance> familyInstances, Autodesk.Revit.DB.Document doc, IList<Pipe> pipes, double distance, List<FamilySymbol> fsList)
		{
			bool result = false;
			IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
			List<YJKLine> list2 = new List<YJKLine>();
			List<XYZ> list3 = new List<XYZ>();
			foreach (Pipe piping in pipes)
			{
				YJKPiping<Pipe> item = new YJKPiping<Pipe>(piping);
				list.Add(item);
			}
			YJKPiping<Pipe> hypiping = null;
			new XYZ();
			XYZ xyz = new XYZ();
			foreach (YJKPiping<Pipe> hypiping2 in list)
			{
				hypiping = hypiping2;
				XYZ origin = hypiping2.StartConnector.Origin;
				xyz = hypiping2.EndConnector.Origin;
                //origin - xyz;
				IEnumerator<YJKPiping<Pipe>> enumerator3 = list.GetEnumerator();
				{
					while (enumerator3.MoveNext())
					{
						YJKPiping<Pipe> hypiping3 = enumerator3.Current;
						if (!hypiping2.IsParallel(hypiping3.Piping))
						{
							throw new Exception("多个设备批量连接时，所有水管须平行");
						}
						YJKLine item2 = new YJKLine(hypiping3.StartConnector.Origin, hypiping3.EndConnector.Origin);
						list2.Add(item2);
						if (!hypiping3.StartConnector.Connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(hypiping2.StartConnector.Connector.CoordinateSystem.BasisZ))
						{
							list3.Add(hypiping3.StartConnector.Origin);
						}
						else
						{
							list3.Add(hypiping3.EndConnector.Origin);
						}
					}
					break;
				}
			}
			IList<EquipmentCoilDouble> list4 = new List<EquipmentCoilDouble>();
			for (int i = 0; i < familyInstances.Count - 1; i++)
			{
				bool flag = false;
				for (int j = i + 1; j < familyInstances.Count; j++)
				{
					if (this.IsAlignEquipment(familyInstances[i], familyInstances[j], hypiping))
					{
						list4.Add(new EquipmentCoilDouble(familyInstances[i], familyInstances[j], true, this.PointToFiDistance(familyInstances[i], hypiping)));
						flag = true;
						break;
					}
				}
				if (!flag && !this.IsExistInEquipmentDouble(list4, familyInstances[i]))
				{
					list4.Add(new EquipmentCoilDouble(familyInstances[i], null, false, this.PointToFiDistance(familyInstances[i], hypiping)));
				}
				if (!flag && i == familyInstances.Count - 2 && !this.IsExistInEquipmentDouble(list4, familyInstances[i + 1]))
				{
					list4.Add(new EquipmentCoilDouble(familyInstances[i + 1], null, false, this.PointToFiDistance(familyInstances[i + 1], hypiping)));
				}
			}
			list4 = (from c in list4
			orderby c.Position
			select c).ToList<EquipmentCoilDouble>();
			foreach (EquipmentCoilDouble equipmentCoilDouble in list4)
			{
				if (equipmentCoilDouble.IsDouble)
				{
					list.Clear();
					foreach (Pipe piping2 in pipes)
					{
						list.Add(new YJKPiping<Pipe>(piping2));
					}
					IList<YJKPiping<Pipe>> orderPipes = this.EquipmentConnect(equipmentCoilDouble, list, fsList);
					this.PipesConnect(orderPipes, (equipmentCoilDouble.ElemFirst as Element).Document, ref pipes, false, 0.0, null);
				}
				else if (!this.ValveEquipmentConn(equipmentCoilDouble.ElemFirst as FamilyInstance, doc, pipes, distance, fsList))
				{
					return false;
				}
				IList<Pipe> list5 = new List<Pipe>();
				foreach (YJKLine hyline in list2)
				{
					foreach (Pipe pipe in pipes)
					{
						bool flag2 = false;
						YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipe);
						if (hyline.IsLineOnLine(hypiping4.GetLocationCurve() as Line))
						{
							foreach (XYZ xyz2 in list3)
							{
								if (xyz2.IsAlmostEqualTo(hypiping4.StartConnector.Origin) || xyz2.IsAlmostEqualTo(hypiping4.EndConnector.Origin))
								{
									list5.Add(pipe);
									flag2 = true;
									break;
								}
							}
							if (flag2)
							{
								break;
							}
						}
					}
				}
				if (list5.Count == list2.Count)
				{
					pipes = list5;
				}
				else if (list5.Count > 0)
				{
					int count = list5.Count;
					int count2 = list2.Count;
				}
				result = true;
			}
			return result;
		}

		private IList<YJKPiping<Pipe>> PipesAndConnectorOrderByType(IList<Connector> connectors, IList<Connector> connsEquipment, IList<Pipe> pipes, Autodesk.Revit.DB.Document doc, int eqType = 0)
		{
			IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
			IList<Pipe> list2 = new List<Pipe>();
			foreach (Pipe item in pipes)
			{
				list2.Add(item);
			}
			foreach (Connector connector in connsEquipment)
			{
				foreach (Connector connector2 in connectors)
				{
					XYZ origin = connector2.Origin;
					FamilyInstance fi = connector2.Owner as FamilyInstance;
					foreach (Pipe pipe in list2)
					{
						YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
						if (connector.PipeSystemType == hypiping.GetConnectorPipingSystemType().pipeSystemType && !connector2.IsConnected)
						{
							double distance = 0.2;
							if (eqType == 1)
							{
								distance = this.GetDistanceSystemTypeSet(connector.PipeSystemType);
							}
							XYZ connectorDirectionPoint = this.GetConnectorDirectionPoint(connector2, distance);
							Pipe pipe2 = YJKRevitTools.NewPipe(hypiping.Piping, connectorDirectionPoint, origin);
							this.PipeSetParameter(connector2, pipe2);
							doc.Regenerate();
							this.PipeConnectFamilyInstance(fi, pipe2);
							list.Add(hypiping);
							list.Add(new YJKPiping<Pipe>(pipe2));
							list2.Remove(pipe);
							break;
						}
					}
				}
			}
			return list;
		}

		public double GetDistanceSystemTypeSet(PipeSystemType systemName)
		{
			double result = 0.82020997375328086;
			try
			{
				EquipmentCoilSetParam equipmentCoilSetText = new HVACUserSetting().GetEquipmentCoilSetText();
                if ((int)systemName != 7)
				{
                    if ((int)systemName != 8)
					{
                        if ((int)systemName == 22)
						{
							result = UnitConvert.LengthToStd(equipmentCoilSetText.CoolWater / 1000.0);
						}
						else
						{
							result = UnitConvert.LengthToStd(equipmentCoilSetText.CoolWater / 1000.0);
						}
					}
					else
					{
						result = UnitConvert.LengthToStd(equipmentCoilSetText.ReturnWater / 1000.0);
					}
				}
				else
				{
					result = UnitConvert.LengthToStd(equipmentCoilSetText.SupplyWater / 1000.0);
				}
			}
			catch (Exception)
			{
			}
			return result;
		}

		private int RelativeDirectionOfPipeAndConn(Pipe pipe, Connector conn)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ xyz = new XYZ(hypiping.EndConnector.Origin.X, hypiping.EndConnector.Origin.Y, hypiping.StartConnector.Origin.Z);
			XYZ xyz2 = hypiping.StartConnector.Origin.Subtract(xyz).Normalize();
			XYZ basisZ = conn.CoordinateSystem.BasisZ;
			double num = xyz2.AngleTo(basisZ);
			int result;
			if (num <= 0.0001 || Math.Abs(num - Math.PI) <= 0.0001)
			{
				result = 0;
			}
			else if (Math.Abs(num - Math.PI*.5) <= 0.0001 || Math.Abs(num - 4.71238898038469) <= 0.0001)
			{
				result = 1;
			}
			else
			{
				result = 2;
			}
			return result;
		}

		private IList<YJKPiping<Pipe>> PipesAndConnectorOrderByType(IList<Connector> connectors, IList<Pipe> pipes, Autodesk.Revit.DB.Document doc, string equipmentType)
		{
			IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
			IList<Pipe> list2 = new List<Pipe>();
			foreach (Pipe item in pipes)
			{
				list2.Add(item);
			}
			foreach (Connector connector in connectors)
			{
				XYZ origin = connector.Origin;
				FamilyInstance familyInstance = connector.Owner as FamilyInstance;
				foreach (Pipe pipe in list2)
				{
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
                    if (connector.PipeSystemType == hypiping.GetConnectorPipingSystemType().pipeSystemType && !connector.IsConnected && (int)connector.Direction == 1)
					{
						double num = this.CheckPositionPipeAndConn(connector, hypiping);
						bool flag = false;
						XYZ connectorDirectionPoint;
						if (num < 0.8 && num > 0.3)
						{
							connectorDirectionPoint = this.GetConnectorDirectionPoint(connector, Math.Abs(num));
							flag = true;
						}
						else
						{
							PumpCoilSetParam pumpValveText = new HVACUserSetting().GetPumpValveText();
							connectorDirectionPoint = this.GetConnectorDirectionPoint(connector, UnitConvert.LengthToStd(pumpValveText.SupplyWater / 1000.0));
						}
						if (hypiping.PipingPositionType == 1)
						{
							flag = true;
						}
						Pipe pipe2 = YJKRevitTools.NewPipe(hypiping.Piping, connectorDirectionPoint, origin);
						this.PipeSetParameter(connector, pipe2);
						doc.Regenerate();
						this.PipeConnectFamilyInstance(familyInstance, pipe2);
						Pipe pipe3 = null;
						XYZ handOrientation = familyInstance.HandOrientation;
						if (YJKRevitTools.IsPlumbVector(handOrientation, hypiping.StartConnector.Origin, hypiping.EndConnector.Origin))
						{
							if (hypiping.PipingPositionType == 1)
							{
								XYZ vectorPointToHPipe = this.GetVectorPointToHPipe(origin, hypiping, handOrientation);
								if (vectorPointToHPipe != null)
								{
								}
							}
							else
							{
								XYZ xyvectorPointToPiping = hypiping.GetXYVectorPointToPiping(origin);
								if (xyvectorPointToPiping != null)
								{
								}
							}
						}
						XYZ xyz = connectorDirectionPoint;
						IList<Pipe> list3 = new List<Pipe>();
						list3.Add(pipe2);
						if (!flag)
						{
							XYZ endXYZ;
							if (num > 0.5)
							{
								endXYZ = xyz + 0.9 * new XYZ(0.0, 0.0, -1.0);
							}
							else
							{
								endXYZ = xyz + 0.9 * new XYZ(0.0, 0.0, 1.0);
							}
							pipe3 = YJKRevitTools.NewPipe(hypiping.Piping, xyz, endXYZ);
							this.PipeSetParameter(connector, pipe3);
							list3.Add(pipe3);
							this.NewElbowFitting(list3, true);
						}
						list.Add(hypiping);
						list.Add(new YJKPiping<Pipe>(pipe3));
						list2.Remove(pipe);
						break;
					}
				}
			}
			return list;
		}

		private IList<YJKPiping<Pipe>> PipesAndConnectorOrderByType2(IList<Connector> connectors, IList<Pipe> pipes, Autodesk.Revit.DB.Document doc, string equipmentType, string conType = "1")
		{
			IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
			IList<Pipe> list2 = new List<Pipe>();
			foreach (Pipe item in pipes)
			{
				list2.Add(item);
			}
			XYZ xyz = (connectors[0].Origin + connectors[1].Origin) / 2.0;
			foreach (Connector connector in connectors)
			{
				XYZ origin = connector.Origin;
				FamilyInstance familyInstance = connector.Owner as FamilyInstance;
				foreach (Pipe pipe in list2)
				{
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
					if (hypiping.PipingPositionType == 1)
					{
						XYZ xyz2 = new XYZ(hypiping.StartConnector.Origin.X, hypiping.StartConnector.Origin.Y, origin.Z);
						if ((connector.Origin - xyz).Normalize().DotProduct((xyz2 - xyz).Normalize()) <= 0.0)
						{
							continue;
						}
					}
                    if (!connector.IsConnected && (int)connector.Direction == 1)
					{
						double num = this.CheckPositionPipeAndConn(connector, hypiping);
						bool flag = false;
						XYZ connectorDirectionPoint;
						if (num < 0.8 && num > 0.3)
						{
							connectorDirectionPoint = this.GetConnectorDirectionPoint(connector, Math.Abs(num));
							flag = true;
						}
						else
						{
							connectorDirectionPoint = this.GetConnectorDirectionPoint(connector, 0.5);
						}
						if (hypiping.PipingPositionType == 1)
						{
							flag = true;
						}
						Pipe pipe2 = YJKRevitTools.NewPipe(hypiping.Piping, connectorDirectionPoint, origin);
						this.PipeSetParameter(connector, pipe2);
						doc.Regenerate();
						this.PipeConnectFamilyInstance(familyInstance, pipe2);
						Pipe pipe4;
						if (conType == "1" || hypiping.PipingPositionType == 1)
						{
							XYZ xyz3 = familyInstance.HandOrientation;
							if (YJKRevitTools.IsPlumbVector(xyz3, hypiping.StartConnector.Origin, hypiping.EndConnector.Origin))
							{
								if (hypiping.PipingPositionType == 1)
								{
									XYZ vectorPointToHPipe = this.GetVectorPointToHPipe(origin, hypiping, xyz3);
									if (vectorPointToHPipe != null)
									{
										xyz3 = vectorPointToHPipe;
									}
								}
								else
								{
									XYZ xyz4 = (connector.Origin - xyz).Normalize();
									if (xyz4 != null)
									{
										xyz3 = xyz4;
									}
								}
							}
							else
							{
								XYZ xyz5 = (connector.Origin - xyz).Normalize();
								if (xyz5 != null)
								{
									xyz3 = xyz5;
								}
							}
							XYZ xyz6 = connectorDirectionPoint + 1.8 * xyz3;
							Pipe pipe3 = YJKRevitTools.NewPipe(hypiping.Piping, connectorDirectionPoint, xyz6);
							this.PipeSetParameter(connector, pipe3);
							IList<Pipe> list3 = new List<Pipe>();
							list3.Add(pipe2);
							list3.Add(pipe3);
							this.NewElbowFitting(list3, true);
							if (flag)
							{
								pipe4 = pipe3;
							}
							else
							{
								XYZ endXYZ;
								if (num > 0.5)
								{
									endXYZ = xyz6 + 0.9 * new XYZ(0.0, 0.0, -1.0);
								}
								else
								{
									endXYZ = xyz6 + 0.9 * new XYZ(0.0, 0.0, 1.0);
								}
								pipe4 = YJKRevitTools.NewPipe(hypiping.Piping, xyz6, endXYZ);
								this.PipeSetParameter(connector, pipe3);
								list3.Clear();
								list3.Add(pipe4);
								list3.Add(pipe3);
								this.NewElbowFitting(list3, true);
							}
						}
						else
						{
							pipe4 = pipe2;
						}
						list.Add(hypiping);
						list.Add(new YJKPiping<Pipe>(pipe4));
						list2.Remove(pipe);
						break;
					}
				}
			}
			return list;
		}

		private IList<YJKPiping<Pipe>> PipesAndConnectorOrderByType(Connector conn, Pipe pipeItem, Autodesk.Revit.DB.Document doc, string equipmentType, string conType = "1")
		{
			IList<YJKPiping<Pipe>> list = new List<YJKPiping<Pipe>>();
			XYZ origin = conn.Origin;
			FamilyInstance familyInstance = conn.Owner as FamilyInstance;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipeItem);
            if (!conn.IsConnected && (int)conn.Direction == 1)
			{
				double num = this.CheckPositionPipeAndConn(conn, hypiping);
				bool flag = false;
				XYZ connectorDirectionPoint;
				if (num < 0.8 && num > 0.3)
				{
					connectorDirectionPoint = this.GetConnectorDirectionPoint(conn, Math.Abs(num));
					flag = true;
				}
				else
				{
					connectorDirectionPoint = this.GetConnectorDirectionPoint(conn, 0.5);
				}
				if (hypiping.PipingPositionType == 1)
				{
					flag = true;
				}
				Pipe pipe = YJKRevitTools.NewPipe(hypiping.Piping, connectorDirectionPoint, origin);
				this.PipeSetParameter(conn, pipe);
				doc.Regenerate();
				this.PipeConnectFamilyInstance(familyInstance, pipe);
				Pipe pipe3;
				if (conType == "1" || hypiping.PipingPositionType == 1)
				{
					XYZ xyz = familyInstance.HandOrientation;
					if (YJKRevitTools.IsPlumbVector(xyz, hypiping.StartConnector.Origin, hypiping.EndConnector.Origin))
					{
						if (hypiping.PipingPositionType == 1)
						{
							XYZ vectorPointToHPipe = this.GetVectorPointToHPipe(origin, hypiping, xyz);
							if (vectorPointToHPipe != null)
							{
								xyz = vectorPointToHPipe;
							}
						}
						else
						{
							XYZ xyvectorPointToPiping = hypiping.GetXYVectorPointToPiping(origin);
							if (xyvectorPointToPiping != null)
							{
								xyz = xyvectorPointToPiping;
							}
						}
					}
					XYZ xyz2 = connectorDirectionPoint + 1.8 * xyz;
					Pipe pipe2 = YJKRevitTools.NewPipe(hypiping.Piping, connectorDirectionPoint, xyz2);
					this.PipeSetParameter(conn, pipe2);
					IList<Pipe> list2 = new List<Pipe>();
					list2.Add(pipe);
					list2.Add(pipe2);
					this.NewElbowFitting(list2, true);
					if (flag)
					{
						pipe3 = pipe2;
					}
					else
					{
						XYZ endXYZ;
						if (num > 0.5)
						{
							endXYZ = xyz2 + 0.9 * new XYZ(0.0, 0.0, -1.0);
						}
						else
						{
							endXYZ = xyz2 + 0.9 * new XYZ(0.0, 0.0, 1.0);
						}
						pipe3 = YJKRevitTools.NewPipe(hypiping.Piping, xyz2, endXYZ);
						this.PipeSetParameter(conn, pipe2);
						list2.Clear();
						list2.Add(pipe3);
						list2.Add(pipe2);
						this.NewElbowFitting(list2, true);
					}
				}
				else
				{
					pipe3 = pipe;
				}
				list.Add(hypiping);
				list.Add(new YJKPiping<Pipe>(pipe3));
			}
			return list;
		}

		private XYZ GetVectorPointToHPipe(XYZ point, YJKPiping<Pipe> hyPipe, XYZ handVctor)
		{
			XYZ xyz = (new XYZ(hyPipe.StartConnector.Origin.X, hyPipe.StartConnector.Origin.Y, point.Z) - point).Normalize();
			XYZ result;
			if (handVctor.DotProduct(xyz) > 0.0)
			{
				result = handVctor;
			}
			else
			{
				result = -handVctor;
			}
			return result;
		}

		private double CheckPositionPipeAndConn(Connector conn, YJKPiping<Pipe> hyPipe)
		{
			if (!conn.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0)))
			{
				return 0.0;
			}
			if (Math.Abs(hyPipe.StartConnector.Origin.Z - hyPipe.EndConnector.Origin.Z) > 0.0001)
			{
				return 0.0;
			}
			return conn.Origin.Z - hyPipe.StartConnector.Origin.Z;
		}

		private void PipeConnectFamilyInstance(FamilyInstance fi, Pipe pipe)
		{
			ConnectHelper connectHelper = new ConnectHelper();
			IList<Connector> pipeConnectorEnd = connectHelper.GetPipeConnectorEnd(pipe);
			IList<Connector> hyelementConnector = connectHelper.GetYJKElementConnector(fi, (Autodesk.Revit.DB.Domain)3);
			bool flag = false;
			foreach (Connector connector in pipeConnectorEnd)
			{
				foreach (Connector connector2 in hyelementConnector)
				{
					if (connector.Origin.IsAlmostEqualTo(connector2.Origin))
					{
						connector.ConnectTo(connector2);
						flag = true;
						break;
					}
				}
				if (flag)
				{
					break;
				}
			}
		}

		private void PipeConnectFamilyInstance(FamilyInstance fi, Pipe pipe, bool bFlex, double flexLength, Autodesk.Revit.DB.Document doc)
		{
			ConnectHelper connectHelper = new ConnectHelper();
			IEnumerable<Connector> pipeConnectorEnd = connectHelper.GetPipeConnectorEnd(pipe);
			IList<Connector> hyelementConnector = connectHelper.GetYJKElementConnector(fi, (Autodesk.Revit.DB.Domain)3);
			foreach (Connector connector in pipeConnectorEnd)
			{
				foreach (Connector connector2 in hyelementConnector)
				{
					if (bFlex)
					{
						if (Math.Abs(connector.Origin.DistanceTo(connector2.Origin) - UnitConvert.LengthToStd(flexLength / 1000.0)) < 0.001)
						{
							FamilyInstance familyInstance = null;
							FamilySymbol ductFittingFamilySymbolByFamilyNameAndSymbol = this.GetDuctFittingFamilySymbolByFamilyNameAndSymbol("波纹管", "标准", doc);
							UnifiedModified.ActivateFamilySymbol(ductFittingFamilySymbolByFamilyNameAndSymbol);
							doc.Regenerate();
							List<XYZ> list = new List<XYZ>();
							list.Add((connector2.Origin - connector.Origin) / 2.0 + connector.Origin);
							ElementSet elementSet = connectHelper.LayFamilyInstance(list, ductFittingFamilySymbolByFamilyNameAndSymbol, 0, doc);
							foreach (object obj in elementSet)
							{
								familyInstance = (obj as FamilyInstance);
								familyInstance.GetParameter("波纹管长度").Set(UnitConvert.LengthToStd(flexLength / 1000.0));
							}
							this.RotateElementbyDuctVec((connector.Origin - hyelementConnector.GetNearestConnector(connector.Origin).Origin).Normalize(), doc, familyInstance, connectHelper, list.FirstOrDefault<XYZ>());
							doc.Regenerate();
							YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
							IEnumerator enumerator3 = elementSet.GetEnumerator();
							{
								if (enumerator3.MoveNext())
								{
									Element element = (Element)enumerator3.Current;
									XYZ moveVector = ((element as FamilyInstance).Location as LocationPoint).Point.Subtract(list.First<XYZ>());
									hyrotateFamilyInstance.MoveElem(moveVector, element as FamilyInstance);
								}
							}
							connectHelper.GetYJKElementConnector(familyInstance, (Autodesk.Revit.DB.Domain)3).GetNearestConnector(connector2.Origin).ConnectTo(connector2);
							connectHelper.GetYJKElementConnector(familyInstance, (Autodesk.Revit.DB.Domain)3).GetNearestConnector(connector.Origin).ConnectTo(connector);
							break;
						}
					}
					else if (connector.Origin.IsAlmostEqualTo(connector2.Origin))
					{
						connector.ConnectTo(connector2);
						break;
					}
				}
			}
		}

		private void RotateElementbyDuctVec(XYZ ductVec, Autodesk.Revit.DB.Document doc, FamilyInstance fdi, ConnectHelper cm, XYZ location)
		{
			Line line = doc.CreatYJKLine(location, new XYZ(0.0, 0.0, 1.0), false);
			double num = ductVec.AngleTo(new XYZ(1.0, 0.0, 0.0));
			ElementTransformUtils.RotateElement(doc, fdi.Id, line, -num);
			IList<Connector> hyelementConnector = cm.GetYJKElementConnector(fdi, (Autodesk.Revit.DB.Domain)3);
			if (!ductVec.IsAlmostEqualTo(hyelementConnector[0].CoordinateSystem.BasisZ, 0.0001) && !ductVec.IsAlmostEqualTo(-hyelementConnector[0].CoordinateSystem.BasisZ, 0.0001))
			{
				num = Math.PI*2 - 2.0 * num;
				ElementTransformUtils.RotateElement(doc, fdi.Id, line, -num);
			}
		}

		private FamilySymbol GetDuctFittingFamilySymbolByFamilyNameAndSymbol(string FamilyName, string SymbolName, Autodesk.Revit.DB.Document doc)
		{
			FamilySymbol familySymbol = null;
			foreach (Element element in YJKElementFilter.GetElementsOfType(doc, typeof(FamilySymbol), BuiltInCategory.OST_PipeAccessory))
			{
				if ((element as FamilySymbol).Family.Name.Contains(FamilyName))
				{
					familySymbol = (element as FamilySymbol);
				}
			}
			if (familySymbol == null)
			{
				SubTransaction subTransaction = null;
				try
				{
					string text = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(doc), "Fitting", FamilyName + ".rfa");
					subTransaction = new SubTransaction(doc);
					subTransaction.Start();
					FamilySymbol familySymbol2;
					doc.LoadFamilySymbol(text, SymbolName, out familySymbol2);
					familySymbol = familySymbol2;
					subTransaction.Commit();
				}
				catch (Exception ex)
				{
					ex.ToString();
				}
				finally
				{
					if (subTransaction != null && (int)subTransaction.GetStatus() != 3)
					{
						subTransaction.RollBack();
					}
				}
			}
			return familySymbol;
		}

		public bool PipeRoundBeam(IList<Pipe> pipes, IList<XYZ> xyzs, double offSet, double angle, ICollection<ElementId> copyElem)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (pipes.Count < 2 || xyzs.Count < 2)
				{
					throw new System.ArgumentNullException("pipes");
				}
				if (pipes[0].Id != pipes[1].Id)
				{
					throw new System.ArgumentNullException("pipes");
				}
				if (0.0 == offSet)
				{
					result = true;
				}
				else
				{
					double num = Math.Tan(angle);
					if (num < 0.0001)
					{
						num = 0.0;
					}
					double num2 = 0.0;
					if (0.0 != num)
					{
						num2 = Math.Abs(offSet / num);
					}
					if (Math.Abs(num2) < 0.0001)
					{
						num2 = 0.0;
					}
					if (num2 * 2.0 >= xyzs[0].DistanceTo(xyzs[1]))
					{
						result = false;
					}
					else
					{
						Document document = pipes[0].Document;
						YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
						subTransaction = new SubTransaction(document);
						subTransaction.Start();
						if (!hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[0], true)) && !hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[1], true)))
						{
							throw new Exception("选择错误，升降点必须至少有一个在水管上有投影。");
						}
						if (!hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[0], true)) && hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[1], true)))
						{
							XYZ origin = hypiping.StartConnector.Origin;
							XYZ origin2 = hypiping.EndConnector.Origin;
							PipeType pipeType = hypiping.Piping.PipeType;
							XYZ globalOnCenterLinePoint = hypiping.GetGlobalOnCenterLinePoint(xyzs[1]);
							XYZ xyz;
							if (xyzs[0].DistanceTo(origin2) > xyzs[0].DistanceTo(origin))
							{
								xyz = origin;
							}
							else
							{
								xyz = origin2;
							}
							XYZ xyz2 = (xyz - globalOnCenterLinePoint).Normalize();
							XYZ xyz3 = new XYZ();
							XYZ endXYZ = new XYZ();
							xyz3 = globalOnCenterLinePoint + xyz2 * num2 + offSet * XYZ.BasisZ;
							endXYZ = xyz + xyz2 * num2 + offSet * XYZ.BasisZ;
							IList<Pipe> list = hypiping.Separate(globalOnCenterLinePoint);
							document.Regenerate();
							Pipe pipe = YJKRevitTools.NewPipe(list[0], globalOnCenterLinePoint, xyz3);
							Pipe pipe2 = YJKRevitTools.NewPipe(list[1], xyz3, endXYZ);
							this.ParameterConvert(list[0], pipe, document);
							this.ParameterConvert(list[1], pipe2, document);
							HYPipeConnect hypipeConnect = new HYPipeConnect();
							double num3 = (list[0].Location as LocationCurve).Curve.GetEndPoint(0).X / 2.0 + (list[0].Location as LocationCurve).Curve.GetEndPoint(1).X / 2.0 - (pipe.Location as LocationCurve).Curve.GetEndPoint(0).X;
							double num4 = (pipe2.Location as LocationCurve).Curve.GetEndPoint(0).X / 2.0 + (pipe2.Location as LocationCurve).Curve.GetEndPoint(1).X / 2.0 - (pipe.Location as LocationCurve).Curve.GetEndPoint(0).X;
							Pipe item;
							Pipe pipe3;
							if (num3 * num4 < 0.0)
							{
								item = list[0];
								pipe3 = list[1];
							}
							else
							{
								item = list[1];
								pipe3 = list[0];
							}
							hypipeConnect.NewElbowFitting(new List<Pipe>
							{
								item,
								pipe
							}, true);
							hypipeConnect.NewElbowFitting(new List<Pipe>
							{
								pipe,
								pipe2
							}, true);
							document.Delete(pipe3.Id);
							YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
							if (copyElem.Count > 0)
							{
								XYZ hyendPoint = hypiping2.GetLocationCurve().GetEndPoint(0);
								XYZ hyendPoint2 = hypiping2.GetLocationCurve().GetEndPoint(1);
								XYZ middlePoint = this.getMiddlePoint(hyendPoint, hyendPoint2);
								XYZ middlePoint2 = this.getMiddlePoint(hyendPoint, middlePoint);
								XYZ middlePoint3 = this.getMiddlePoint(hyendPoint2, middlePoint);
								IList<Pipe> source = hypiping2.Separate(middlePoint2);
								document.Regenerate();
								YJKPiping<Pipe> item2 = null;
								YJKPiping<Pipe> item3 = null;
								Pipe pipe4 = null;
								if (this.getMiddlePoint(source.ElementAt(0)).DistanceTo(middlePoint) > hyendPoint.DistanceTo(hyendPoint2) / 4.0)
								{
									item2 = new YJKPiping<Pipe>(source.ElementAt(0));
									IList<Pipe> source2 = new YJKPiping<Pipe>(source.ElementAt(1)).Separate(middlePoint3);
									document.Regenerate();
									if (this.getMiddlePoint(source2.ElementAt(0)).DistanceTo(middlePoint) > hyendPoint.DistanceTo(hyendPoint2) / 4.0)
									{
										item3 = new YJKPiping<Pipe>(source2.ElementAt(0));
										pipe4 = source2.ElementAt(1);
									}
									else
									{
										item3 = new YJKPiping<Pipe>(source2.ElementAt(1));
										pipe4 = source2.ElementAt(0);
									}
								}
								else if (this.getMiddlePoint(source.ElementAt(0)).DistanceTo(middlePoint) < hyendPoint.DistanceTo(hyendPoint2) / 4.0)
								{
									item2 = new YJKPiping<Pipe>(source.ElementAt(1));
									IList<Pipe> source3 = new YJKPiping<Pipe>(source.ElementAt(0)).Separate(middlePoint3);
									document.Regenerate();
									if (this.getMiddlePoint(source3.ElementAt(0)).DistanceTo(middlePoint) < hyendPoint.DistanceTo(hyendPoint2) / 4.0)
									{
										item3 = new YJKPiping<Pipe>(source3.ElementAt(1));
										pipe4 = source3.ElementAt(0);
									}
									else
									{
										item3 = new YJKPiping<Pipe>(source3.ElementAt(0));
										pipe4 = source3.ElementAt(1);
									}
								}
								List<YJKPiping<Pipe>> list2 = new List<YJKPiping<Pipe>>();
								list2.Add(item2);
								list2.Add(item3);
								document.Delete(pipe4.Id);
								document.Regenerate();
								XYZ xyz4 = new XYZ(-100.0, -100.0, -100.0);
								ElementTransformUtils.MoveElements(document, copyElem, xyz4);
								document.Regenerate();
								ElementTransformUtils.MoveElements(document, copyElem, new XYZ(0.0, 0.0, offSet));
								document.Regenerate();
								XYZ xyz5;
								if (copyElem.Count == 1)
								{
									xyz5 = new XYZ(middlePoint.X - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.X, middlePoint.Y - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Y, middlePoint.Z - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Z);
								}
								else
								{
									xyz5 = new XYZ(middlePoint.X - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.X, middlePoint.Y - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Y, middlePoint.Z - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Z);
								}
								document.Regenerate();
								ElementTransformUtils.MoveElements(document, copyElem, xyz5);
								hypipeConnect.NewDuctAccessory(list2, middlePoint, copyElem, document);
							}
							subTransaction.Commit();
							document.Regenerate();
							result = true;
						}
						else if (hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[0], true)) && !hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[1], true)))
						{
							XYZ origin3 = hypiping.StartConnector.Origin;
							XYZ origin4 = hypiping.EndConnector.Origin;
							PipeType pipeType2 = hypiping.Piping.PipeType;
							XYZ globalOnCenterLinePoint2 = hypiping.GetGlobalOnCenterLinePoint(xyzs[0]);
							XYZ xyz6;
							if (xyzs[1].DistanceTo(origin4) > xyzs[1].DistanceTo(origin3))
							{
								xyz6 = origin3;
							}
							else
							{
								xyz6 = origin4;
							}
							XYZ xyz7 = (xyz6 - globalOnCenterLinePoint2).Normalize();
							XYZ xyz8 = new XYZ();
							XYZ endXYZ2 = new XYZ();
							xyz8 = globalOnCenterLinePoint2 + xyz7 * num2 + offSet * XYZ.BasisZ;
							endXYZ2 = xyz6 + xyz7 * num2 + offSet * XYZ.BasisZ;
							IList<Pipe> list3 = hypiping.Separate(globalOnCenterLinePoint2);
							document.Regenerate();
							Pipe pipe5 = YJKRevitTools.NewPipe(list3[0], globalOnCenterLinePoint2, xyz8);
							Pipe pipe6 = YJKRevitTools.NewPipe(list3[1], xyz8, endXYZ2);
							this.ParameterConvert(list3[0], pipe5, document);
							this.ParameterConvert(list3[1], pipe6, document);
							HYPipeConnect hypipeConnect2 = new HYPipeConnect();
							double num5 = (list3[0].Location as LocationCurve).Curve.GetEndPoint(0).X / 2.0 + (list3[0].Location as LocationCurve).Curve.GetEndPoint(1).X / 2.0 - (pipe5.Location as LocationCurve).Curve.GetEndPoint(0).X;
							double num6 = (pipe6.Location as LocationCurve).Curve.GetEndPoint(0).X / 2.0 + (pipe6.Location as LocationCurve).Curve.GetEndPoint(1).X / 2.0 - (pipe5.Location as LocationCurve).Curve.GetEndPoint(0).X;
							Pipe item4;
							Pipe pipe7;
							if (num5 * num6 < 0.0)
							{
								item4 = list3[0];
								pipe7 = list3[1];
							}
							else
							{
								item4 = list3[1];
								pipe7 = list3[0];
							}
							hypipeConnect2.NewElbowFitting(new List<Pipe>
							{
								item4,
								pipe5
							}, true);
							hypipeConnect2.NewElbowFitting(new List<Pipe>
							{
								pipe5,
								pipe6
							}, true);
							document.Delete(pipe7.Id);
							YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipe6);
							if (copyElem.Count > 0)
							{
								XYZ hyendPoint3 = hypiping3.GetLocationCurve().GetEndPoint(0);
								XYZ hyendPoint4 = hypiping3.GetLocationCurve().GetEndPoint(1);
								XYZ middlePoint4 = this.getMiddlePoint(hyendPoint3, hyendPoint4);
								XYZ middlePoint5 = this.getMiddlePoint(hyendPoint3, middlePoint4);
								XYZ middlePoint6 = this.getMiddlePoint(hyendPoint4, middlePoint4);
								IList<Pipe> source4 = hypiping3.Separate(middlePoint5);
								document.Regenerate();
								YJKPiping<Pipe> item5 = null;
								YJKPiping<Pipe> item6 = null;
								Pipe pipe8 = null;
								if (this.getMiddlePoint(source4.ElementAt(0)).DistanceTo(middlePoint4) > hyendPoint3.DistanceTo(hyendPoint4) / 4.0)
								{
									item5 = new YJKPiping<Pipe>(source4.ElementAt(0));
									IList<Pipe> source5 = new YJKPiping<Pipe>(source4.ElementAt(1)).Separate(middlePoint6);
									document.Regenerate();
									if (this.getMiddlePoint(source5.ElementAt(0)).DistanceTo(middlePoint4) > hyendPoint3.DistanceTo(hyendPoint4) / 4.0)
									{
										item6 = new YJKPiping<Pipe>(source5.ElementAt(0));
										pipe8 = source5.ElementAt(1);
									}
									else
									{
										item6 = new YJKPiping<Pipe>(source5.ElementAt(1));
										pipe8 = source5.ElementAt(0);
									}
								}
								else if (this.getMiddlePoint(source4.ElementAt(0)).DistanceTo(middlePoint4) < hyendPoint3.DistanceTo(hyendPoint4) / 4.0)
								{
									item5 = new YJKPiping<Pipe>(source4.ElementAt(1));
									IList<Pipe> source6 = new YJKPiping<Pipe>(source4.ElementAt(0)).Separate(middlePoint6);
									document.Regenerate();
									if (this.getMiddlePoint(source6.ElementAt(0)).DistanceTo(middlePoint4) < hyendPoint3.DistanceTo(hyendPoint4) / 4.0)
									{
										item6 = new YJKPiping<Pipe>(source6.ElementAt(1));
										pipe8 = source6.ElementAt(0);
									}
									else
									{
										item6 = new YJKPiping<Pipe>(source6.ElementAt(0));
										pipe8 = source6.ElementAt(1);
									}
								}
								List<YJKPiping<Pipe>> list4 = new List<YJKPiping<Pipe>>();
								list4.Add(item5);
								list4.Add(item6);
								document.Delete(pipe8.Id);
								document.Regenerate();
								XYZ xyz9 = new XYZ(-100.0, -100.0, -100.0);
								ElementTransformUtils.MoveElements(document, copyElem, xyz9);
								document.Regenerate();
								ElementTransformUtils.MoveElements(document, copyElem, new XYZ(0.0, 0.0, offSet));
								document.Regenerate();
								XYZ xyz10;
								if (copyElem.Count == 1)
								{
									xyz10 = new XYZ(middlePoint4.X - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.X, middlePoint4.Y - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Y, middlePoint4.Z - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Z);
								}
								else
								{
									xyz10 = new XYZ(middlePoint4.X - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.X, middlePoint4.Y - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Y, middlePoint4.Z - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Z);
								}
								document.Regenerate();
								ElementTransformUtils.MoveElements(document, copyElem, xyz10);
								hypipeConnect2.NewDuctAccessory(list4, middlePoint4, copyElem, document);
							}
							subTransaction.Commit();
							document.Regenerate();
							result = true;
						}
						else
						{
							XYZ origin5 = hypiping.StartConnector.Origin;
							XYZ origin6 = hypiping.EndConnector.Origin;
							PipeType pipeType3 = hypiping.Piping.PipeType;
							XYZ globalOnCenterLinePoint3 = hypiping.GetGlobalOnCenterLinePoint(xyzs[0]);
							XYZ globalOnCenterLinePoint4 = hypiping.GetGlobalOnCenterLinePoint(xyzs[1]);
							XYZ xyz11 = (globalOnCenterLinePoint4 - globalOnCenterLinePoint3).Normalize();
							XYZ xyz12 = new XYZ();
							XYZ xyz13 = new XYZ();
							xyz12 = globalOnCenterLinePoint3 + xyz11 * num2 + offSet * XYZ.BasisZ;
							xyz13 = globalOnCenterLinePoint4 - xyz11 * num2 + offSet * XYZ.BasisZ;
							IList<Pipe> list5 = hypiping.Separate(globalOnCenterLinePoint3);
							document.Regenerate();
							YJKPiping<Pipe> hypiping4;
							YJKPiping<Pipe> hypiping5;
							YJKPiping<Pipe> hypiping6;
							if (hypiping.IsPointOnPipingCurve(globalOnCenterLinePoint4))
							{
								hypiping4 = new YJKPiping<Pipe>(list5[1]);
								IList<Pipe> list6 = hypiping.Separate(globalOnCenterLinePoint4);
								document.Regenerate();
								if (hypiping.StartConnector.Origin.IsAlmostEqualTo(origin5) || hypiping.StartConnector.Origin.IsAlmostEqualTo(origin6) || hypiping.EndConnector.Origin.IsAlmostEqualTo(origin5) || hypiping.EndConnector.Origin.IsAlmostEqualTo(origin6))
								{
									hypiping5 = new YJKPiping<Pipe>(list6[0]);
									hypiping6 = new YJKPiping<Pipe>(list6[1]);
								}
								else
								{
									hypiping5 = new YJKPiping<Pipe>(list6[1]);
									hypiping6 = new YJKPiping<Pipe>(list6[0]);
								}
							}
							else
							{
								YJKPiping<Pipe> hypiping7 = new YJKPiping<Pipe>(list5[1]);
								hypiping4 = new YJKPiping<Pipe>(list5[0]);
								IList<Pipe> list7 = hypiping7.Separate(globalOnCenterLinePoint4);
								document.Regenerate();
								if (hypiping7.StartConnector.Origin.IsAlmostEqualTo(origin5) || hypiping7.StartConnector.Origin.IsAlmostEqualTo(origin6) || hypiping7.EndConnector.Origin.IsAlmostEqualTo(origin5) || hypiping7.EndConnector.Origin.IsAlmostEqualTo(origin6))
								{
									hypiping5 = new YJKPiping<Pipe>(list7[0]);
									hypiping6 = new YJKPiping<Pipe>(list7[1]);
								}
								else
								{
									hypiping5 = new YJKPiping<Pipe>(list7[1]);
									hypiping6 = new YJKPiping<Pipe>(list7[0]);
								}
							}
							document.Delete(hypiping6.Piping.Id);
							document.Regenerate();
							Pipe pipe9 = YJKRevitTools.NewPipe(hypiping4.Piping, globalOnCenterLinePoint3, xyz12);
							Pipe pipe10 = YJKRevitTools.NewPipe(hypiping4.Piping, xyz12, xyz13);
							Pipe pipe11 = YJKRevitTools.NewPipe(hypiping4.Piping, xyz13, globalOnCenterLinePoint4);
							this.ParameterConvert(hypiping4.Piping, pipe9, document);
							this.ParameterConvert(hypiping4.Piping, pipe10, document);
							this.ParameterConvert(hypiping4.Piping, pipe11, document);
							HYPipeConnect hypipeConnect3 = new HYPipeConnect();
							hypipeConnect3.NewElbowFitting(new List<Pipe>
							{
								hypiping4.Piping,
								pipe9
							}, true);
							hypipeConnect3.NewElbowFitting(new List<Pipe>
							{
								pipe9,
								pipe10
							}, true);
							hypipeConnect3.NewElbowFitting(new List<Pipe>
							{
								pipe10,
								pipe11
							}, true);
							hypipeConnect3.NewElbowFitting(new List<Pipe>
							{
								pipe11,
								hypiping5.Piping
							}, true);
							YJKPiping<Pipe> hypiping8 = new YJKPiping<Pipe>(pipe10);
							if (copyElem.Count > 0)
							{
								XYZ hyendPoint5 = hypiping8.GetLocationCurve().GetEndPoint(0);
								XYZ hyendPoint6 = hypiping8.GetLocationCurve().GetEndPoint(1);
								XYZ middlePoint7 = this.getMiddlePoint(hyendPoint5, hyendPoint6);
								XYZ middlePoint8 = this.getMiddlePoint(hyendPoint5, middlePoint7);
								XYZ middlePoint9 = this.getMiddlePoint(hyendPoint6, middlePoint7);
								IList<Pipe> source7 = hypiping8.Separate(middlePoint8);
								document.Regenerate();
								YJKPiping<Pipe> item7 = null;
								YJKPiping<Pipe> item8 = null;
								Pipe pipe12 = null;
								if (this.getMiddlePoint(source7.ElementAt(0)).DistanceTo(middlePoint7) > hyendPoint5.DistanceTo(hyendPoint6) / 4.0)
								{
									item7 = new YJKPiping<Pipe>(source7.ElementAt(0));
									IList<Pipe> source8 = new YJKPiping<Pipe>(source7.ElementAt(1)).Separate(middlePoint9);
									document.Regenerate();
									if (this.getMiddlePoint(source8.ElementAt(0)).DistanceTo(middlePoint7) > hyendPoint5.DistanceTo(hyendPoint6) / 4.0)
									{
										item8 = new YJKPiping<Pipe>(source8.ElementAt(0));
										pipe12 = source8.ElementAt(1);
									}
									else
									{
										item8 = new YJKPiping<Pipe>(source8.ElementAt(1));
										pipe12 = source8.ElementAt(0);
									}
								}
								else if (this.getMiddlePoint(source7.ElementAt(0)).DistanceTo(middlePoint7) < hyendPoint5.DistanceTo(hyendPoint6) / 4.0)
								{
									item7 = new YJKPiping<Pipe>(source7.ElementAt(1));
									IList<Pipe> source9 = new YJKPiping<Pipe>(source7.ElementAt(0)).Separate(middlePoint9);
									document.Regenerate();
									if (this.getMiddlePoint(source9.ElementAt(0)).DistanceTo(middlePoint7) < hyendPoint5.DistanceTo(hyendPoint6) / 4.0)
									{
										item8 = new YJKPiping<Pipe>(source9.ElementAt(1));
										pipe12 = source9.ElementAt(0);
									}
									else
									{
										item8 = new YJKPiping<Pipe>(source9.ElementAt(0));
										pipe12 = source9.ElementAt(1);
									}
								}
								List<YJKPiping<Pipe>> list8 = new List<YJKPiping<Pipe>>();
								list8.Add(item7);
								list8.Add(item8);
								document.Delete(pipe12.Id);
								document.Regenerate();
								XYZ xyz14 = new XYZ(-100.0, -100.0, -100.0);
								ElementTransformUtils.MoveElements(document, copyElem, xyz14);
								document.Regenerate();
								ElementTransformUtils.MoveElements(document, copyElem, new XYZ(0.0, 0.0, offSet));
								document.Regenerate();
								XYZ xyz15;
								if (copyElem.Count == 1)
								{
									xyz15 = new XYZ(middlePoint7.X - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.X, middlePoint7.Y - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Y, middlePoint7.Z - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Z);
								}
								else
								{
									xyz15 = new XYZ(middlePoint7.X - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.X, middlePoint7.Y - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Y, middlePoint7.Z - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Z);
								}
								document.Regenerate();
								ElementTransformUtils.MoveElements(document, copyElem, xyz15);
								hypipeConnect3.NewDuctAccessory(list8, middlePoint7, copyElem, document);
							}
							subTransaction.Commit();
							document.Regenerate();
							result = true;
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null && (int)subTransaction.GetStatus() != 3)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private XYZ getMiddlePoint(XYZ xyzone, XYZ xyztwo)
		{
			return new XYZ((xyzone.X + xyztwo.X) / 2.0, (xyzone.Y + xyztwo.Y) / 2.0, (xyzone.Z + xyztwo.Z) / 2.0);
		}

		private XYZ getMiddlePoint(Pipe hyODuct)
		{
			XYZ hyendPoint = (hyODuct.Location as LocationCurve).Curve.GetEndPoint(0);
			XYZ hyendPoint2 = (hyODuct.Location as LocationCurve).Curve.GetEndPoint(1);
			return new XYZ((hyendPoint.X + hyendPoint2.X) / 2.0, (hyendPoint.Y + hyendPoint2.Y) / 2.0, (hyendPoint.Z + hyendPoint2.Z) / 2.0);
		}

		public bool PipeRoundBeam(IList<Pipe> pipes, IList<XYZ> xyzs, double offSet, double angle)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (pipes.Count < 2 || xyzs.Count < 2)
				{
					throw new System.ArgumentNullException("pipes");
				}
				if (pipes[0].Id != pipes[1].Id)
				{
					throw new System.ArgumentNullException("pipes");
				}
				if (0.0 == offSet)
				{
					result = true;
				}
				else
				{
					double num = Math.Tan(angle);
					if (num < 0.0001)
					{
						num = 0.0;
					}
					double num2 = 0.0;
					if (0.0 != num)
					{
						num2 = Math.Abs(offSet / num);
					}
					if (Math.Abs(num2) < 0.0001)
					{
						num2 = 0.0;
					}
					if (num2 * 2.0 >= xyzs[0].DistanceTo(xyzs[1]))
					{
						result = false;
					}
					else
					{
						Document document = pipes[0].Document;
						YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
						subTransaction = new SubTransaction(document);
						subTransaction.Start();
						if (!hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[0], true)) && !hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[1], true)))
						{
							throw new Exception("选择错误，升降点必须至少有一个在水管上有投影。");
						}
						if (!hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[0], true)) && hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[1], true)))
						{
							XYZ origin = hypiping.StartConnector.Origin;
							XYZ origin2 = hypiping.EndConnector.Origin;
							PipeType pipeType = hypiping.Piping.PipeType;
							XYZ globalOnCenterLinePoint = hypiping.GetGlobalOnCenterLinePoint(xyzs[1]);
							XYZ xyz;
							if (xyzs[0].DistanceTo(origin2) > xyzs[0].DistanceTo(origin))
							{
								xyz = origin;
							}
							else
							{
								xyz = origin2;
							}
							XYZ xyz2 = (xyz - globalOnCenterLinePoint).Normalize();
							XYZ xyz3 = new XYZ();
							XYZ endXYZ = new XYZ();
							xyz3 = globalOnCenterLinePoint + xyz2 * num2 + offSet * XYZ.BasisZ;
							endXYZ = xyz + xyz2 * num2 + offSet * XYZ.BasisZ;
							IList<Pipe> list = hypiping.Separate(globalOnCenterLinePoint);
							document.Regenerate();
							Pipe pipe = YJKRevitTools.NewPipe(list[0], globalOnCenterLinePoint, xyz3);
							Pipe pipe2 = YJKRevitTools.NewPipe(list[1], xyz3, endXYZ);
							this.ParameterConvert(list[0], pipe, document);
							this.ParameterConvert(list[1], pipe2, document);
							HYPipeConnect hypipeConnect = new HYPipeConnect();
							double num3 = (list[0].Location as LocationCurve).Curve.GetEndPoint(0).X / 2.0 + (list[0].Location as LocationCurve).Curve.GetEndPoint(1).X / 2.0 - (pipe.Location as LocationCurve).Curve.GetEndPoint(0).X;
							double num4 = (pipe2.Location as LocationCurve).Curve.GetEndPoint(0).X / 2.0 + (pipe2.Location as LocationCurve).Curve.GetEndPoint(1).X / 2.0 - (pipe.Location as LocationCurve).Curve.GetEndPoint(0).X;
							Pipe item;
							Pipe pipe3;
							if (num3 * num4 < 0.0)
							{
								item = list[0];
								pipe3 = list[1];
							}
							else
							{
								item = list[1];
								pipe3 = list[0];
							}
							hypipeConnect.NewElbowFitting(new List<Pipe>
							{
								item,
								pipe
							}, true);
							hypipeConnect.NewElbowFitting(new List<Pipe>
							{
								pipe,
								pipe2
							}, true);
							document.Delete(pipe3.Id);
							subTransaction.Commit();
							document.Regenerate();
							result = true;
						}
						else if (hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[0], true)) && !hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[1], true)))
						{
							XYZ origin3 = hypiping.StartConnector.Origin;
							XYZ origin4 = hypiping.EndConnector.Origin;
							PipeType pipeType2 = hypiping.Piping.PipeType;
							XYZ globalOnCenterLinePoint2 = hypiping.GetGlobalOnCenterLinePoint(xyzs[0]);
							XYZ xyz4;
							if (xyzs[1].DistanceTo(origin4) > xyzs[1].DistanceTo(origin3))
							{
								xyz4 = origin3;
							}
							else
							{
								xyz4 = origin4;
							}
							XYZ xyz5 = (xyz4 - globalOnCenterLinePoint2).Normalize();
							XYZ xyz6 = new XYZ();
							XYZ endXYZ2 = new XYZ();
							xyz6 = globalOnCenterLinePoint2 + xyz5 * num2 + offSet * XYZ.BasisZ;
							endXYZ2 = xyz4 + xyz5 * num2 + offSet * XYZ.BasisZ;
							IList<Pipe> list2 = hypiping.Separate(globalOnCenterLinePoint2);
							document.Regenerate();
							Pipe pipe4 = YJKRevitTools.NewPipe(list2[0], globalOnCenterLinePoint2, xyz6);
							Pipe pipe5 = YJKRevitTools.NewPipe(list2[1], xyz6, endXYZ2);
							this.ParameterConvert(list2[0], pipe4, document);
							this.ParameterConvert(list2[1], pipe5, document);
							HYPipeConnect hypipeConnect2 = new HYPipeConnect();
							double num5 = (list2[0].Location as LocationCurve).Curve.GetEndPoint(0).X / 2.0 + (list2[0].Location as LocationCurve).Curve.GetEndPoint(1).X / 2.0 - (pipe4.Location as LocationCurve).Curve.GetEndPoint(0).X;
							double num6 = (pipe5.Location as LocationCurve).Curve.GetEndPoint(0).X / 2.0 + (pipe5.Location as LocationCurve).Curve.GetEndPoint(1).X / 2.0 - (pipe4.Location as LocationCurve).Curve.GetEndPoint(0).X;
							Pipe item2;
							Pipe pipe6;
							if (num5 * num6 < 0.0)
							{
								item2 = list2[0];
								pipe6 = list2[1];
							}
							else
							{
								item2 = list2[1];
								pipe6 = list2[0];
							}
							hypipeConnect2.NewElbowFitting(new List<Pipe>
							{
								item2,
								pipe4
							}, true);
							hypipeConnect2.NewElbowFitting(new List<Pipe>
							{
								pipe4,
								pipe5
							}, true);
							document.Delete(pipe6.Id);
							subTransaction.Commit();
							document.Regenerate();
							result = true;
						}
						else
						{
							XYZ origin5 = hypiping.StartConnector.Origin;
							XYZ origin6 = hypiping.EndConnector.Origin;
							PipeType pipeType3 = hypiping.Piping.PipeType;
							XYZ globalOnCenterLinePoint3 = hypiping.GetGlobalOnCenterLinePoint(xyzs[0]);
							XYZ globalOnCenterLinePoint4 = hypiping.GetGlobalOnCenterLinePoint(xyzs[1]);
							XYZ xyz7 = (globalOnCenterLinePoint4 - globalOnCenterLinePoint3).Normalize();
							XYZ xyz8 = new XYZ();
							XYZ xyz9 = new XYZ();
							xyz8 = globalOnCenterLinePoint3 + xyz7 * num2 + offSet * XYZ.BasisZ;
							xyz9 = globalOnCenterLinePoint4 - xyz7 * num2 + offSet * XYZ.BasisZ;
							IList<Pipe> list3 = hypiping.Separate(globalOnCenterLinePoint3);
							document.Regenerate();
							YJKPiping<Pipe> hypiping2;
							YJKPiping<Pipe> hypiping3;
							YJKPiping<Pipe> hypiping4;
							if (hypiping.IsPointOnPipingCurve(globalOnCenterLinePoint4))
							{
								hypiping2 = new YJKPiping<Pipe>(list3[1]);
								IList<Pipe> list4 = hypiping.Separate(globalOnCenterLinePoint4);
								document.Regenerate();
								if (hypiping.StartConnector.Origin.IsAlmostEqualTo(origin5) || hypiping.StartConnector.Origin.IsAlmostEqualTo(origin6) || hypiping.EndConnector.Origin.IsAlmostEqualTo(origin5) || hypiping.EndConnector.Origin.IsAlmostEqualTo(origin6))
								{
									hypiping3 = new YJKPiping<Pipe>(list4[0]);
									hypiping4 = new YJKPiping<Pipe>(list4[1]);
								}
								else
								{
									hypiping3 = new YJKPiping<Pipe>(list4[1]);
									hypiping4 = new YJKPiping<Pipe>(list4[0]);
								}
							}
							else
							{
								YJKPiping<Pipe> hypiping5 = new YJKPiping<Pipe>(list3[1]);
								hypiping2 = new YJKPiping<Pipe>(list3[0]);
								IList<Pipe> list5 = hypiping5.Separate(globalOnCenterLinePoint4);
								document.Regenerate();
								if (hypiping5.StartConnector.Origin.IsAlmostEqualTo(origin5) || hypiping5.StartConnector.Origin.IsAlmostEqualTo(origin6) || hypiping5.EndConnector.Origin.IsAlmostEqualTo(origin5) || hypiping5.EndConnector.Origin.IsAlmostEqualTo(origin6))
								{
									hypiping3 = new YJKPiping<Pipe>(list5[0]);
									hypiping4 = new YJKPiping<Pipe>(list5[1]);
								}
								else
								{
									hypiping3 = new YJKPiping<Pipe>(list5[1]);
									hypiping4 = new YJKPiping<Pipe>(list5[0]);
								}
							}
							document.Delete(hypiping4.Piping.Id);
							document.Regenerate();
							Pipe pipe7 = YJKRevitTools.NewPipe(hypiping2.Piping, globalOnCenterLinePoint3, xyz8);
							Pipe pipe8 = YJKRevitTools.NewPipe(hypiping2.Piping, xyz8, xyz9);
							Pipe pipe9 = YJKRevitTools.NewPipe(hypiping2.Piping, xyz9, globalOnCenterLinePoint4);
							this.ParameterConvert(hypiping2.Piping, pipe7, document);
							this.ParameterConvert(hypiping2.Piping, pipe8, document);
							this.ParameterConvert(hypiping2.Piping, pipe9, document);
							HYPipeConnect hypipeConnect3 = new HYPipeConnect();
							hypipeConnect3.NewElbowFitting(new List<Pipe>
							{
								hypiping2.Piping,
								pipe7
							}, true);
							hypipeConnect3.NewElbowFitting(new List<Pipe>
							{
								pipe7,
								pipe8
							}, true);
							hypipeConnect3.NewElbowFitting(new List<Pipe>
							{
								pipe8,
								pipe9
							}, true);
							hypipeConnect3.NewElbowFitting(new List<Pipe>
							{
								pipe9,
								hypiping3.Piping
							}, true);
							subTransaction.Commit();
							document.Regenerate();
							result = true;
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null && (int)subTransaction.GetStatus() != 3)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public void NewPipePIANYI(Pipe pipe, XYZ xyz1, Pipe pipe2, XYZ xyz2, double distance, double angle, string model)
		{
			Document document = pipe.Document;
			SubTransaction subTransaction = new SubTransaction(document);
			if (model == "升降")
			{
				if (Math.Abs(distance) < 3.0 * this.getPipeDiameter(pipe))
				{
					DialogResult dialogResult = this.IsDistanceOK("升降高度");
					if (dialogResult == DialogResult.Yes)
					{
						if (distance < 0.0)
						{
							distance = -3.0 * this.getPipeDiameter(pipe);
						}
						else
						{
							distance = 3.0 * this.getPipeDiameter(pipe);
						}
					}
					else if (dialogResult != DialogResult.No)
					{
						return;
					}
				}
			}
			else if (Math.Abs(distance) < 3.0 * this.getPipeDiameter(pipe))
			{
				DialogResult dialogResult2 = this.IsDistanceOK("偏移距离");
				if (dialogResult2 == DialogResult.Yes)
				{
					if (distance < 0.0)
					{
						distance = -3.0 * this.getPipeDiameter(pipe);
					}
					else
					{
						distance = 3.0 * this.getPipeDiameter(pipe);
					}
				}
				else if (dialogResult2 != DialogResult.No)
				{
					return;
				}
			}
			if (model == "升降")
			{
				Parameter parameter = pipe.GetParameter(BuiltInParameter.RBS_PIPE_SLOPE);
				Parameter parameter2 = pipe2.GetParameter(BuiltInParameter.RBS_PIPE_SLOPE);
				if (((int)parameter.StorageType == 2 && parameter.AsDouble() > 0.0) || ((int)parameter2.StorageType == 2 && parameter2.AsDouble() > 0.0))
				{
					YJKMessageBox.Show("带坡度的管道不允许升降！");
					return;
				}
			}
			try
			{
				subTransaction.Start();
				if (pipe.Id != pipe2.Id)
				{
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe, xyz1);
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2, xyz2);
					XYZ project = hypiping.GetProject(xyz1);
					XYZ project2 = hypiping2.GetProject(xyz2);
					XYZ xyz3 = new XYZ(100.0, 100.0, 100.0);
					if (hypiping.IsPointOnPipingCurve(xyz3))
					{
						xyz3 = new XYZ(177.0, 258.0, 0.0);
					}
					XYZ project3 = hypiping.GetProject(xyz3, false);
					XYZ xyz4 = (new XYZ(xyz3.X, xyz3.Y, project3.Z) - project3).Normalize();
					XYZ point = hypiping.StartConnector.Connector.Origin + xyz4 * 10.0;
					if (!new HYCreatePipeHelper().judgePointIsAnotherLine(hypiping.GetLocationCurve() as Line, point))
					{
						xyz4 = -1.0 * xyz4.Normalize();
					}
					XYZ xyz5 = null;
					if (hypiping.IsParallel(hypiping2.Piping))
					{
						if (model == "偏移")
						{
							if (distance > 0.0)
							{
								xyz5 = xyz4;
							}
							else
							{
								xyz5 = -1.0 * xyz4;
							}
						}
						if (model == "升降")
						{
							if (distance > 0.0)
							{
								xyz5 = new XYZ(0.0, 0.0, 1.0);
							}
							else
							{
								xyz5 = new XYZ(0.0, 0.0, -1.0);
							}
						}
					}
					else
					{
						if (!(model == "升降"))
						{
							YJKMessageBox.Show("两根管道不平行时候不提供偏移功能！");
							return;
						}
						if (distance > 0.0)
						{
							xyz5 = new XYZ(0.0, 0.0, 1.0);
						}
						else
						{
							xyz5 = new XYZ(0.0, 0.0, -1.0);
						}
					}
					List<Pipe> source = hypiping.Separate(project);
					List<Pipe> source2 = hypiping2.Separate(project2);
					Pipe pipe3;
					Pipe pipe4;
					if (this.getMiddlePoint(source.ElementAt(0)).DistanceTo(project) + this.getMiddlePoint(source.ElementAt(0)).DistanceTo(project2) > this.getMiddlePoint(source.ElementAt(1)).DistanceTo(project) + this.getMiddlePoint(source.ElementAt(1)).DistanceTo(project2))
					{
						pipe3 = source.ElementAt(0);
						pipe4 = source.ElementAt(1);
					}
					else
					{
						pipe3 = source.ElementAt(1);
						pipe4 = source.ElementAt(0);
					}
					Pipe pipe5;
					Pipe pipe6;
					if (this.getMiddlePoint(source2.ElementAt(0)).DistanceTo(project2) + this.getMiddlePoint(source2.ElementAt(0)).DistanceTo(project) > this.getMiddlePoint(source2.ElementAt(1)).DistanceTo(project2) + this.getMiddlePoint(source2.ElementAt(1)).DistanceTo(project))
					{
						pipe5 = source2.ElementAt(0);
						pipe6 = source2.ElementAt(1);
					}
					else
					{
						pipe5 = source2.ElementAt(1);
						pipe6 = source2.ElementAt(0);
					}
					List<ElementId> list = new List<ElementId>();
					list.Add(pipe4.Id);
					list.Add(pipe6.Id);
					distance = Math.Abs(distance);
					ElementTransformUtils.MoveElements(document, list, xyz5 * Math.Abs(distance));
					document.Regenerate();
					XYZ xyz6;
					XYZ xyz7;
					Pipe pipe7;
					Pipe pipe8;
					if (angle == Math.PI*.5)
					{
						xyz6 = project + xyz5 * Math.Abs(distance);
						xyz7 = project2 + xyz5 * Math.Abs(distance);
						pipe7 = YJKRevitTools.NewPipe(pipe3, project, xyz6);
						pipe8 = YJKRevitTools.NewPipe(pipe5, project2, xyz7);
					}
					else
					{
						xyz6 = project + xyz5 * Math.Abs(distance) + Math.Abs(distance) / Math.Tan(angle) * YJKRevitTools.getLocationCurveDirectionfarAwayfromXYZ(pipe4, project).Normalize();
						xyz7 = project2 + xyz5 * Math.Abs(distance) + Math.Abs(distance) / Math.Tan(angle) * YJKRevitTools.getLocationCurveDirectionfarAwayfromXYZ(pipe6, project2).Normalize();
						pipe7 = YJKRevitTools.NewPipe(pipe3, project, xyz6);
						pipe8 = YJKRevitTools.NewPipe(pipe5, project2, xyz7);
					}
					YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipe4);
					YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipe6);
					if ((!hypiping3.IsPointOnPipingCurve(xyz6) || !hypiping4.IsPointOnPipingCurve(xyz7)) && MessageBox.Show("由于偏移角度过小或者其他原因可能造成连接出错，是否强行执行？", "盈建科提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) != DialogResult.Yes)
					{
						subTransaction.RollBack();
					}
					else
					{
						this.ElbowConnect(pipe3, pipe7, project, project);
						this.ElbowConnect(pipe7, pipe4, xyz6, project);
						this.ElbowConnect(pipe5, pipe8, project2, project2);
						this.ElbowConnect(pipe8, pipe6, xyz7, project2);
					}
				}
				else
				{
					YJKMessageBox.Show("两个点都必须在选择的管道上有投影点，才能执行两侧偏移！");
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.EndTransation(subTransaction, false);
			}
			finally
			{
				this.EndTransation(subTransaction, true);
			}
		}

		public bool NewPipePIANYI(Pipe pipe, XYZ xyz1, XYZ xyz2, double distance, double angle, string model, bool isDirectSJ = false)
		{
			ElementId id = pipe.Id;
			bool result = false;
			Document document = pipe.Document;
			double num = 3.0;
			if (!isDirectSJ)
			{
				if (model == "升降")
				{
					if (Math.Abs(distance) < num * this.getPipeDiameter(pipe))
					{
						DialogResult dialogResult = this.IsDistanceOK("升降高度");
						if (dialogResult == DialogResult.Yes)
						{
							if (distance < 0.0)
							{
								distance = -num * this.getPipeDiameter(pipe);
							}
							else
							{
								distance = num * this.getPipeDiameter(pipe);
							}
						}
						else if (dialogResult != DialogResult.No)
						{
							return false;
						}
					}
				}
				else if (Math.Abs(distance) < num * this.getPipeDiameter(pipe))
				{
					DialogResult dialogResult2 = this.IsDistanceOK("偏移距离");
					if (dialogResult2 == DialogResult.OK)
					{
						if (distance < 0.0)
						{
							distance = -num * this.getPipeDiameter(pipe);
						}
						else
						{
							distance = num * this.getPipeDiameter(pipe);
						}
					}
					else if (dialogResult2 != DialogResult.No)
					{
						return false;
					}
				}
			}
			SubTransaction subTransaction = new SubTransaction(document);
			if (model == "升降")
			{
				Parameter parameter = pipe.GetParameter(BuiltInParameter.RBS_PIPE_SLOPE);
				if ((int)parameter.StorageType == 2 && parameter.AsDouble() > 0.0)
				{
					YJKMessageBox.Show("带坡度的管道不允许升降！");
					return false;
				}
			}
			try
			{
				subTransaction.Start();
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				XYZ project = hypiping.GetProject(xyz1);
				XYZ project2 = hypiping.GetProject(xyz2);
				XYZ xyz3 = new XYZ(100.0, 100.0, 100.0);
				if (hypiping.IsPointOnPipingCurve(xyz3))
				{
					xyz3 = new XYZ(177.0, 258.0, 0.0);
				}
				XYZ project3 = hypiping.GetProject(xyz3, false);
				XYZ xyz4 = (new XYZ(xyz3.X, xyz3.Y, project3.Z) - project3).Normalize();
				XYZ point = hypiping.StartConnector.Connector.Origin + xyz4 * 10.0;
				if (!new HYCreatePipeHelper().judgePointIsAnotherLine(hypiping.GetLocationCurve() as Line, point))
				{
					xyz4 = -1.0 * xyz4.Normalize();
				}
				List<Pipe> source = new List<Pipe>();
				source = hypiping.Separate(project);
				new YJKPiping<Pipe>(source.ElementAt(0));
				Pipe pipe2;
				Pipe piping;
				if (new YJKPiping<Pipe>(source.ElementAt(1)).IsPointOnPipingCurve(project2, 1E-06))
				{
					pipe2 = source.ElementAt(0);
					piping = source.ElementAt(1);
				}
				else
				{
					pipe2 = source.ElementAt(1);
					piping = source.ElementAt(0);
				}
				List<Pipe> source2 = new List<Pipe>();
				source2 = new YJKPiping<Pipe>(piping).Separate(project2);
				document.Regenerate();
				Pipe pipe3;
				Pipe pipe4;
				if (this.getMiddlePoint(source2.ElementAt(0)).DistanceTo(project2) + this.getMiddlePoint(source2.ElementAt(0)).DistanceTo(project) > this.getMiddlePoint(source2.ElementAt(1)).DistanceTo(project2) + this.getMiddlePoint(source2.ElementAt(1)).DistanceTo(project))
				{
					pipe3 = source2.ElementAt(0);
					pipe4 = source2.ElementAt(1);
				}
				else
				{
					pipe3 = source2.ElementAt(1);
					pipe4 = source2.ElementAt(0);
				}
				XYZ xyz5 = null;
				if (model == "偏移")
				{
					if (distance > 0.0)
					{
						xyz5 = xyz4;
					}
					else
					{
						xyz5 = -1.0 * xyz4;
					}
				}
				if (model == "升降")
				{
					if (distance > 0.0)
					{
						xyz5 = new XYZ(0.0, 0.0, 1.0);
					}
					else
					{
						xyz5 = new XYZ(0.0, 0.0, -1.0);
					}
				}
				distance = Math.Abs(distance);
				ElementTransformUtils.MoveElement(document, pipe4.Id, xyz5 * Math.Abs(distance));
				document.Regenerate();
				XYZ xyz6;
				XYZ xyz7;
				Pipe pipe5;
				Pipe pipe6;
				if (angle == Math.PI*.5)
				{
					xyz6 = project + xyz5 * Math.Abs(distance);
					xyz7 = project2 + xyz5 * Math.Abs(distance);
					pipe5 = YJKRevitTools.NewPipe(pipe2, project, xyz6);
					pipe6 = YJKRevitTools.NewPipe(pipe2, project2, xyz7);
				}
				else
				{
					xyz6 = project + xyz5 * Math.Abs(distance) + Math.Abs(distance) / Math.Tan(angle) * (project2 - project).Normalize();
					xyz7 = project2 + xyz5 * Math.Abs(distance) + Math.Abs(distance) / Math.Tan(angle) * (project - project2).Normalize();
					pipe5 = YJKRevitTools.NewPipe(pipe2, project, xyz6);
					pipe6 = YJKRevitTools.NewPipe(pipe2, project2, xyz7);
				}
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe4);
				XYZ xyz8 = (hypiping2.EndConnector.Origin + hypiping2.StartConnector.Origin) / 2.0;
				XYZ xyz9 = project + xyz5 * Math.Abs(distance);
				if (xyz6.DistanceTo(xyz8) + xyz9.DistanceTo(xyz6) > xyz9.DistanceTo(xyz8))
				{
					if (new FormPYSJWrong("shuangce").ShowDialog() == DialogResult.OK)
					{
						subTransaction.RollBack();
						document.Regenerate();
						return this.NewPipePIANYI(document.GetElementById(id) as Pipe, xyz1, xyz2, distance, Math.PI*.5, model, false);
					}
					subTransaction.RollBack();
					return false;
				}
				else
				{
					this.ElbowConnect(pipe2, pipe5, project, project);
					this.ElbowConnect(pipe5, pipe4, xyz6, project);
					this.ElbowConnect(pipe4, pipe6, project2, xyz7);
					this.ElbowConnect(pipe6, pipe3, project2, project2);
					result = true;
					subTransaction.Commit();
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.EndTransation(subTransaction, false);
			}
			finally
			{
				this.EndTransation(subTransaction, true);
			}
			return result;
		}

		public bool NewPipePIANYI(Pipe pipe, XYZ xyz1, double distance, double angle, string model, XYZ xyzDirection)
		{
			ElementId id = pipe.Id;
			bool result = false;
			Document document = pipe.Document;
			if (model == "升降")
			{
				if (Math.Abs(distance) < 3.0 * this.getPipeDiameter(pipe))
				{
					DialogResult dialogResult = this.IsDistanceOK("升降高度");
					if (dialogResult == DialogResult.Yes)
					{
						if (distance < 0.0)
						{
							distance = -3.0 * this.getPipeDiameter(pipe);
						}
						else
						{
							distance = 3.0 * this.getPipeDiameter(pipe);
						}
					}
					else if (dialogResult != DialogResult.No)
					{
						return false;
					}
				}
			}
			else if (Math.Abs(distance) < 3.0 * this.getPipeDiameter(pipe))
			{
				DialogResult dialogResult2 = this.IsDistanceOK("偏移距离");
				if (dialogResult2 == DialogResult.Yes)
				{
					if (distance < 0.0)
					{
						distance = -3.0 * this.getPipeDiameter(pipe);
					}
					else
					{
						distance = 3.0 * this.getPipeDiameter(pipe);
					}
				}
				else if (dialogResult2 != DialogResult.No)
				{
					return false;
				}
			}
			SubTransaction subTransaction = new SubTransaction(document);
			if (model == "升降")
			{
				Parameter parameter = pipe.GetParameter(BuiltInParameter.RBS_PIPE_SLOPE);
				if ((int)parameter.StorageType == 2 && parameter.AsDouble() > 0.0)
				{
					YJKMessageBox.Show("带坡度的管道不允许升降！");
					return false;
				}
			}
			try
			{
				subTransaction.Start();
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				XYZ xyz2 = new XYZ(100.0, 100.0, 100.0);
				if (hypiping.IsPointOnPipingCurve(xyz2))
				{
					xyz2 = new XYZ(177.0, 258.0, 0.0);
				}
				XYZ project = hypiping.GetProject(xyz2, false);
				XYZ xyz3 = (new XYZ(xyz2.X, xyz2.Y, project.Z) - project).Normalize();
				XYZ point = hypiping.StartConnector.Connector.Origin + xyz3 * 10.0;
				if (!new HYCreatePipeHelper().judgePointIsAnotherLine(hypiping.GetLocationCurve() as Line, point))
				{
					xyz3 = -1.0 * xyz3.Normalize();
				}
				XYZ project2 = hypiping.GetProject(xyz1);
				XYZ project3 = hypiping.GetProject(xyzDirection, true);
				List<Pipe> source = new List<Pipe>();
				source = hypiping.Separate(project2);
				new YJKPiping<Pipe>(source.ElementAt(0));
				Pipe pipe2;
				Pipe pipe3;
				if (new YJKPiping<Pipe>(source.ElementAt(1)).IsPointOnPipingCurve(project3, 1E-06))
				{
					pipe2 = source.ElementAt(0);
					pipe3 = source.ElementAt(1);
				}
				else
				{
					pipe2 = source.ElementAt(1);
					pipe3 = source.ElementAt(0);
				}
				XYZ xyz4 = null;
				if (model == "偏移")
				{
					if (distance > 0.0)
					{
						xyz4 = xyz3;
					}
					else
					{
						xyz4 = -1.0 * xyz3;
					}
				}
				if (model == "升降")
				{
					if (distance > 0.0)
					{
						xyz4 = new XYZ(0.0, 0.0, 1.0);
					}
					else
					{
						xyz4 = new XYZ(0.0, 0.0, -1.0);
					}
				}
				distance = Math.Abs(distance) + this.getPipeDiameter(pipe2);
				ElementTransformUtils.MoveElement(document, pipe3.Id, xyz4 * Math.Abs(distance));
				XYZ xyz5;
				Pipe pipe4;
				if (angle == Math.PI*.5)
				{
					xyz5 = project2 + xyz4 * Math.Abs(distance);
					pipe4 = YJKRevitTools.NewPipe(pipe2, project2, xyz5);
				}
				else
				{
					xyz5 = project2 + xyz4 * Math.Abs(distance) + Math.Abs(distance) / Math.Tan(angle) * (project3 - project2).Normalize();
					pipe4 = YJKRevitTools.NewPipe(pipe2, project2, xyz5);
				}
				if (!new YJKPiping<Pipe>(pipe3).IsPointOnPipingCurve(xyz5))
				{
					if (new FormPYSJWrong("dance").ShowDialog() == DialogResult.OK)
					{
						subTransaction.RollBack();
						document.Regenerate();
						return this.NewPipePIANYI(document.GetElementById(id) as Pipe, xyz1, distance, Math.PI*.5, model, xyzDirection);
					}
					subTransaction.RollBack();
					return false;
				}
				else
				{
					this.ElbowConnect(pipe2, pipe4, project2, project2);
					this.ElbowConnect(pipe4, pipe3, xyz5, project2);
					subTransaction.Commit();
					document.Regenerate();
					result = true;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.EndTransation(subTransaction, false);
			}
			finally
			{
				this.EndTransation(subTransaction, true);
			}
			return result;
		}

		private void ElbowConnect(Pipe pipe1, Pipe pipe2, XYZ xyz1, XYZ xyz2)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe1);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
			pipe1.Document.Create.NewElbowFitting(hypiping.GetCloseConnector(xyz1, false), hypiping2.GetCloseConnector(xyz2, false));
		}

		private void EndTransation(SubTransaction tran, bool beSure)
		{
			if (tran != null)
			{
				TransactionStatus status = tran.GetStatus();
				if ((int)status == 1)
				{
					if (beSure)
					{
						tran.Commit();
						return;
					}
					tran.RollBack();
					return;
				}
				else if ((int)status == 5)
				{
					tran.RollBack();
				}
			}
		}

		private List<ElementNode> GetAllElemNode(ElementNode eNode, ref List<ElementNode> listENode)
		{
			if (eNode == null)
			{
				return listENode;
			}
			listENode.Add(eNode);
			this.GetAllElemNode(eNode.Next, ref listENode);
			if (eNode.SubElementNode != null)
			{
				foreach (ElementNode eNode2 in eNode.SubElementNode)
				{
					this.GetAllElemNode(eNode2, ref listENode);
				}
			}
			return listENode;
		}

		public void AutoPipeRoundBeam(IList<Element> listElement, double newangle, double offset)
		{
			for (int i = 0; i < listElement.Count; i++)
			{
				if (listElement.ElementAt(i) is Pipe)
				{
					Pipe pipe = listElement.ElementAt(i) as Pipe;
					new YJKLine((pipe.Location as LocationCurve).Curve.GetEndPoint(0), (pipe.Location as LocationCurve).Curve.GetEndPoint(1));
				}
				if (listElement.ElementAt(i) is Duct)
				{
					Duct duct = listElement.ElementAt(i) as Duct;
					new YJKLine((duct.Location as LocationCurve).Curve.GetEndPoint(0), (duct.Location as LocationCurve).Curve.GetEndPoint(1));
				}
				for (int j = 1; j < listElement.Count; j++)
				{
					if (listElement.ElementAt(j) is Pipe)
					{
						Curve curve = ((listElement.ElementAt(j) as Pipe).Location as LocationCurve).Curve;
					}
					if (listElement.ElementAt(j) is Duct)
					{
						Curve curve2 = ((listElement.ElementAt(j) as Duct).Location as LocationCurve).Curve;
					}
					if (YJKRevitTools.IsElementIntersectsElement(listElement.ElementAt(i), listElement.ElementAt(j)))
					{
						if (listElement.ElementAt(i) is Pipe && listElement.ElementAt(j) is Duct)
						{
							YJKLine hyline = new YJKLine(YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(i) as Pipe));
							XYZ globalPoint = hyline.IntersectionPoint(YJKRevitTools.getIntersectLine(YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(i) as Pipe), YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(j) as Duct)));
							HYPipeConnect hypipeConnect = new HYPipeConnect();
							IList<XYZ> xyzs = new List<XYZ>();
							IList<Pipe> list = new List<Pipe>();
							double ductWidth = this.getDuctWidth(listElement.ElementAt(j) as Duct);
							double ductHeight = this.getDuctHeight(listElement.ElementAt(j) as Duct);
							double pipeDiameter = this.getPipeDiameter(listElement.ElementAt(i) as Pipe);
							double width = (listElement.ElementAt(j) as Duct).Width;
							this.getPipeDiameter(listElement.ElementAt(i) as Pipe);
							list.Add(listElement.ElementAt(i) as Pipe);
							list.Add(listElement.ElementAt(i) as Pipe);
							if (offset > 0.0)
							{
								offset += (ductHeight + pipeDiameter) / 2.0;
							}
							else if (offset < 0.0)
							{
								offset -= (ductHeight + pipeDiameter) / 2.0;
							}
							double num;
							if (newangle == Math.PI*.5)
							{
								num = (ductWidth + 2.0 * pipeDiameter) / 2.0;
							}
							else
							{
								num = Math.Abs(offset) / Math.Tan(newangle) + ductWidth / 2.0 + pipeDiameter;
							}
							if (num < ductWidth)
							{
								YJKMessageBox.Show("升降角度过大，要使升降过程增加的管件可以放下！");
								return;
							}
							if (Math.Abs(offset) < ductHeight)
							{
								YJKMessageBox.Show("升降高度过低,请加大高度！");
								return;
							}
							xyzs = hyline.GetLineXYZList(globalPoint, num);
							hypipeConnect.PipeRoundBeam(list, xyzs, offset, newangle);
							i = listElement.Count;
							j = listElement.Count;
						}
						else if (listElement.ElementAt(i) is Duct && listElement.ElementAt(j) is Pipe)
						{
							YJKLine hyline2 = new YJKLine(YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(j) as Pipe));
							XYZ globalPoint = hyline2.IntersectionPoint(YJKRevitTools.getIntersectLine(YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(j) as Pipe), YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(i) as Duct)));
							HYPipeConnect hypipeConnect2 = new HYPipeConnect();
							IList<XYZ> xyzs2 = new List<XYZ>();
							IList<Pipe> list2 = new List<Pipe>();
							double ductWidth2 = this.getDuctWidth(listElement.ElementAt(i) as Duct);
							double ductHeight2 = this.getDuctHeight(listElement.ElementAt(i) as Duct);
							double pipeDiameter2 = this.getPipeDiameter(listElement.ElementAt(j) as Pipe);
							double width2 = (listElement.ElementAt(i) as Duct).Width;
							this.getPipeDiameter(listElement.ElementAt(j) as Pipe);
							list2.Add(listElement.ElementAt(j) as Pipe);
							list2.Add(listElement.ElementAt(j) as Pipe);
							if (offset > 0.0)
							{
								offset += (ductHeight2 + pipeDiameter2) / 2.0;
							}
							else if (offset < 0.0)
							{
								offset -= (ductHeight2 + pipeDiameter2) / 2.0;
							}
							double num2;
							if (newangle == Math.PI*.5)
							{
								num2 = (ductWidth2 + 2.0 * pipeDiameter2) / 2.0;
							}
							else
							{
								num2 = Math.Abs(offset) / Math.Tan(newangle) + ductWidth2 / 2.0 + pipeDiameter2;
							}
							if (num2 < ductWidth2)
							{
								YJKMessageBox.Show("升降角度过大，要使升降过程增加的管件可以放下！");
								return;
							}
							if (Math.Abs(offset) < ductHeight2)
							{
								YJKMessageBox.Show("升降高度过低,请加大高度！");
								return;
							}
							xyzs2 = hyline2.GetLineXYZList(globalPoint, num2);
							hypipeConnect2.PipeRoundBeam(list2, xyzs2, offset, newangle);
							i = listElement.Count;
							j = listElement.Count;
						}
						else if (listElement.ElementAt(i) is Pipe && listElement.ElementAt(j) is Pipe)
						{
							if (this.getPipeDiameter(listElement.ElementAt(i) as Pipe) <= this.getPipeDiameter(listElement.ElementAt(j) as Pipe))
							{
								YJKLine hyline3 = new YJKLine(YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(i) as Pipe));
								XYZ globalPoint = hyline3.IntersectionPoint(YJKRevitTools.getIntersectLine(YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(i) as Pipe), YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(j) as Pipe)));
								HYPipeConnect hypipeConnect3 = new HYPipeConnect();
								IList<XYZ> xyzs3 = new List<XYZ>();
								IList<Pipe> list3 = new List<Pipe>();
								double pipeDiameter3 = this.getPipeDiameter(listElement.ElementAt(j) as Pipe);
								double pipeDiameter4 = this.getPipeDiameter(listElement.ElementAt(j) as Pipe);
								double pipeDiameter5 = this.getPipeDiameter(listElement.ElementAt(i) as Pipe);
								this.getPipeDiameter(listElement.ElementAt(i) as Pipe);
								list3.Add(listElement.ElementAt(i) as Pipe);
								list3.Add(listElement.ElementAt(i) as Pipe);
								if (offset > 0.0)
								{
									offset += (pipeDiameter4 + pipeDiameter5) / 2.0;
								}
								else if (offset < 0.0)
								{
									offset -= (pipeDiameter4 + pipeDiameter5) / 2.0;
								}
								double num3;
								if (newangle == Math.PI*.5)
								{
									num3 = (pipeDiameter3 + 2.0 * pipeDiameter5) / 2.0;
								}
								else
								{
									num3 = Math.Abs(offset) / Math.Tan(newangle) + pipeDiameter3 / 2.0 + pipeDiameter5;
								}
								if (num3 < pipeDiameter3)
								{
									YJKMessageBox.Show("升降角度过大，要使升降过程增加的管件可以放下！");
									return;
								}
								if (Math.Abs(offset) < pipeDiameter4)
								{
									YJKMessageBox.Show("升降高度过低,请加大高度！");
									return;
								}
								xyzs3 = hyline3.GetLineXYZList(globalPoint, num3);
								hypipeConnect3.PipeRoundBeam(list3, xyzs3, offset, newangle);
								i = listElement.Count;
								j = listElement.Count;
							}
							else
							{
								YJKLine hyline4 = new YJKLine(YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(j) as Pipe));
								XYZ globalPoint = hyline4.IntersectionPoint(YJKRevitTools.getIntersectLine(YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(j) as Pipe), YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(i) as Pipe)));
								HYPipeConnect hypipeConnect4 = new HYPipeConnect();
								IList<XYZ> xyzs4 = new List<XYZ>();
								IList<Pipe> list4 = new List<Pipe>();
								double pipeDiameter6 = this.getPipeDiameter(listElement.ElementAt(i) as Pipe);
								double pipeDiameter7 = this.getPipeDiameter(listElement.ElementAt(i) as Pipe);
								double pipeDiameter8 = this.getPipeDiameter(listElement.ElementAt(j) as Pipe);
								this.getPipeDiameter(listElement.ElementAt(j) as Pipe);
								list4.Add(listElement.ElementAt(j) as Pipe);
								list4.Add(listElement.ElementAt(j) as Pipe);
								if (offset > 0.0)
								{
									offset += (pipeDiameter7 + pipeDiameter8) / 2.0;
								}
								else if (offset < 0.0)
								{
									offset -= (pipeDiameter7 + pipeDiameter8) / 2.0;
								}
								double num4;
								if (newangle == Math.PI*.5)
								{
									num4 = (pipeDiameter6 + 2.0 * pipeDiameter8) / 2.0;
								}
								else
								{
									num4 = Math.Abs(offset) / Math.Tan(newangle) + pipeDiameter6 / 2.0 + pipeDiameter8;
								}
								if (num4 < pipeDiameter6)
								{
									YJKMessageBox.Show("升降角度过大，要使升降过程增加的管件可以放下！");
									return;
								}
								if (Math.Abs(offset) < pipeDiameter7)
								{
									YJKMessageBox.Show("升降高度过低,请加大高度！");
									return;
								}
								xyzs4 = hyline4.GetLineXYZList(globalPoint, num4);
								hypipeConnect4.PipeRoundBeam(list4, xyzs4, offset, newangle);
								i = listElement.Count;
								j = listElement.Count;
							}
						}
						else if (listElement.ElementAt(i) is Duct && listElement.ElementAt(j) is Duct)
						{
							if (this.getDuctHeight(listElement.ElementAt(i) as Duct) <= this.getDuctHeight(listElement.ElementAt(j) as Duct))
							{
								YJKLine hyline5 = new YJKLine(YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(i) as Duct));
								XYZ globalPoint = hyline5.IntersectionPoint(YJKRevitTools.getIntersectLine(YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(i) as Duct), YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(j) as Duct)));
								HYDuctAlignment hyductAlignment = new HYDuctAlignment();
								IList<XYZ> xyzs5 = new List<XYZ>();
								IList<Duct> list5 = new List<Duct>();
								double ductWidth3 = this.getDuctWidth(listElement.ElementAt(j) as Duct);
								double ductHeight3 = this.getDuctHeight(listElement.ElementAt(j) as Duct);
								double ductHeight4 = this.getDuctHeight(listElement.ElementAt(i) as Duct);
								double width3 = (listElement.ElementAt(j) as Duct).Width;
								this.getDuctHeight(listElement.ElementAt(i) as Duct);
								list5.Add(listElement.ElementAt(i) as Duct);
								list5.Add(listElement.ElementAt(i) as Duct);
								if (offset > 0.0)
								{
									offset += (ductHeight3 + ductHeight4) / 2.0;
								}
								else if (offset < 0.0)
								{
									offset -= (ductHeight3 + ductHeight4) / 2.0;
								}
								double num5;
								if (newangle == Math.PI*.5)
								{
									num5 = (ductWidth3 + 2.0 * ductHeight4) / 2.0;
								}
								else
								{
									num5 = Math.Abs(offset) / Math.Tan(newangle) + ductWidth3 / 2.0 + ductHeight4;
								}
								if (num5 < ductWidth3)
								{
									YJKMessageBox.Show("升降角度过大，要使升降过程增加的管件可以放下！");
									return;
								}
								if (Math.Abs(offset) < ductHeight3)
								{
									YJKMessageBox.Show("升降高度过低,请加大高度！");
									return;
								}
								xyzs5 = hyline5.GetLineXYZList(globalPoint, num5);
								hyductAlignment.DuctRoundBeam(list5, xyzs5, offset, newangle);
								i = listElement.Count;
								j = listElement.Count;
							}
							else
							{
								YJKLine hyline6 = new YJKLine(YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(j) as Duct));
								XYZ globalPoint = hyline6.IntersectionPoint(YJKRevitTools.getIntersectLine(YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(j) as Duct), YJKRevitTools.getDuctOrPipeLine(listElement.ElementAt(i) as Duct)));
								HYDuctAlignment hyductAlignment2 = new HYDuctAlignment();
								IList<XYZ> xyzs6 = new List<XYZ>();
								IList<Duct> list6 = new List<Duct>();
								double ductWidth4 = this.getDuctWidth(listElement.ElementAt(i) as Duct);
								double ductHeight5 = this.getDuctHeight(listElement.ElementAt(i) as Duct);
								double ductHeight6 = this.getDuctHeight(listElement.ElementAt(j) as Duct);
								double width4 = (listElement.ElementAt(i) as Duct).Width;
								this.getDuctHeight(listElement.ElementAt(j) as Duct);
								list6.Add(listElement.ElementAt(j) as Duct);
								list6.Add(listElement.ElementAt(j) as Duct);
								if (offset > 0.0)
								{
									offset += (ductHeight5 + ductHeight6) / 2.0;
								}
								else if (offset < 0.0)
								{
									offset -= (ductHeight5 + ductHeight6) / 2.0;
								}
								double num6;
								if (newangle == Math.PI*.5)
								{
									num6 = (ductWidth4 + 2.0 * ductHeight6) / 2.0;
								}
								else
								{
									num6 = Math.Abs(offset) / Math.Tan(newangle) + ductWidth4 / 2.0 + ductHeight6;
								}
								if (num6 < ductWidth4)
								{
									YJKMessageBox.Show("升降角度过大，要使升降过程增加的管件可以放下！");
									return;
								}
								if (Math.Abs(offset) < ductHeight5)
								{
									YJKMessageBox.Show("升降高度过低,请加大高度！");
									return;
								}
								xyzs6 = hyline6.GetLineXYZList(globalPoint, num6);
								hyductAlignment2.DuctRoundBeam(list6, xyzs6, offset, newangle);
								i = listElement.Count;
								j = listElement.Count;
							}
						}
					}
				}
			}
		}

		private double getPipeDiameter(Pipe pipe)
		{
			return pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
		}

		private double getDuctHeight(Duct duct)
		{
			double result;
			if (new YJKPiping<Duct>(duct).StartConnector.Connector.Shape == ConnectorSharp.Round)
			{
				result = duct.Diameter;
			}
			else
			{
				result = duct.Height;
			}
			return result;
		}

		private double getDuctWidth(Duct duct)
		{
			double result;
			if (new YJKPiping<Duct>(duct).StartConnector.Connector.Shape == ConnectorSharp.Round)
			{
				result = duct.Diameter;
			}
			else
			{
				result = duct.Width;
			}
			return result;
		}

		private void ParameterConvert(Pipe sourcePipe, Pipe newPipe, Autodesk.Revit.DB.Document document)
		{
			SubTransaction subTransaction = new SubTransaction(document);
			subTransaction.Start();
			foreach (object obj in newPipe.Parameters)
			{
				Parameter parameter = (Parameter)obj;
                switch ((int)parameter.StorageType)
				{
				case 1:
					if (!parameter.IsReadOnly)
					{
						parameter.Set(sourcePipe.GetParameter(parameter.Definition).AsInteger());
					}
					break;
				case 2:
					if (parameter.Id.IntegerValue != -1114132 && !parameter.IsReadOnly)
					{
						parameter.Set(sourcePipe.GetParameter(parameter.Definition).AsDouble());
					}
					break;
				case 3:
					if (!parameter.IsReadOnly && parameter.IsShared)
					{
						parameter.Set(sourcePipe.GetParameter(parameter.Definition).AsString());
					}
					break;
				case 4:
					parameter.AsElementId();
					if (!parameter.IsReadOnly)
					{
						parameter.Set(sourcePipe.GetParameter(parameter.Definition).AsElementId());
					}
					break;
				}
			}
            if ((int)subTransaction.Commit() == 5)
			{
				subTransaction.RollBack();
			}
		}

		public YJKPiping<Pipe> CheckPipingsAngleOrCanConnect(YJKPiping<Pipe> hyPipe, YJKPiping<Pipe> hyPipe1, Autodesk.Revit.DB.Document doc, ref bool canConn)
		{
			if (hyPipe.IsParallel(hyPipe1.Piping))
			{
				canConn = true;
				return hyPipe1;
			}
			XYZ connectorOrigin = this.GetConnectorOrigin(hyPipe1.Piping.ConnectorManager.UnusedConnectors);
			XYZ origin = hyPipe1.EndConnector.Origin;
			if (origin.IsAlmostEqualTo(connectorOrigin))
			{
				origin = hyPipe1.StartConnector.Origin;
			}
			XYZ xyz = hyPipe.GetProject(connectorOrigin, true);
			xyz = new XYZ(xyz.X, xyz.Y, connectorOrigin.Z);
			double num = (xyz - connectorOrigin).AngleTo(origin - connectorOrigin);
			if (Math.Abs(180.0 - num / Math.PI * 180.0) < 0.1)
			{
				canConn = true;
				return hyPipe1;
			}
			if (num / Math.PI * 180.0 >= 0.0 && num / Math.PI * 180.0 < 89.9999998)
			{
				canConn = false;
				return hyPipe1;
			}
			canConn = true;
			XYZ endXYZ = connectorOrigin + hyPipe1.GetDiameter() * (xyz - connectorOrigin).Normalize() * 10.0;
			Pipe pipe = YJKRevitTools.NewPipe(hyPipe1.Piping, connectorOrigin, endXYZ);
			doc.Regenerate();
			ElementTransformUtils.MoveElement(doc, pipe.Id, new XYZ(1E-05, 1E-05, 1E-05));
			ElementTransformUtils.MoveElement(doc, pipe.Id, -1.0 * new XYZ(1E-05, 1E-05, 1E-05));
			doc.Regenerate();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			doc.Create.NewElbowFitting(hyPipe1.GetCloseConnector(connectorOrigin, false), hypiping.GetCloseConnector(connectorOrigin, false));
			return new YJKPiping<Pipe>(pipe);
		}

		public XYZ GetConnectorOrigin(ConnectorSet cs)
		{
			XYZ result = null;
			foreach (object obj in cs)
			{
				result = ((Connector)obj).Origin;
			}
			return result;
		}

		private int pipeConnectType = 1;

		private IList<Pipe> listEquipmentLastPipe = new List<Pipe>();
	}
}
